2017-10-12 138 views
0

我想爲我在matplotlib/seaborn中創建的邊緣線和填充分佈圖設置不同級別的透明度(= alpha)。例如:不同級別的邊緣透明度和填充matplotlib或seaborn分佈圖

ax1 = sns.distplot(BSRDI_DF, label="BsrDI", bins=newBins, kde=False, 
        hist_kws={"edgecolor": (1,0,0,1), "color":(1,0,0,0.25)}) 

上述方法不起作用,很遺憾。有沒有人有任何想法我可以做到這一點?

回答

1

編輯沒關係,我想用color代替facecolor是造成問題,但似乎我只拿到了期待權,因爲該補丁是重疊輸出,讓看似黑暗的邊緣。

調查該問題進一步後,it looks like seaborn is hard-setting the alpha level at 0.4,它取代了參數傳遞給hist_kws=

sns.distplot(x, kde=False, hist_kws={"edgecolor": (1,0,0,1), "lw":5, "facecolor":(0,1,0,0.1), "rwidth":0.8}) 

enter image description here

在使用相同的參數plt.hist()給出:

plt.hist(x, edgecolor=(1,0,0,1), lw=5, facecolor=(0,1,0,0.1), rwidth=0.8) 

enter image description here

結論:如果你想要邊緣和臉部顏色不同的alpha級別,你必須直接使用matplotlib,而不是seaborn。

+0

這也正是從這個問題的代碼。雖然問題並不清楚究竟什麼不起作用,但解決方案不能重新發布問題中的代碼。 – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest事實上,事實並非如此。 OP使用'edgecolor'和'color',而正確的做法是時間使用'edgecolor'和'** face ** color'。我想我應該指出我的回答中的差異 –

+0

@ImportanceOfBeingErnest沒關係,我的答案在所有 –

1

問題似乎是seaborn爲直方圖設置了alpha參數。雖然alpha默認爲None用於通常的直方圖,使得像

plt.hist(x, lw=3, edgecolor=(1,0,0,0.75), color=(1,0,0,0.25)) 

按預期工作,seaborn設置該阿爾法某些給定值。這將覆蓋在RGBA元組中設置的alpha。

的解決方案是明確地設定α位None

ax = sns.distplot(x, kde=False, hist_kws={"lw":3, "edgecolor": (1,0,0,0.75), 
                "color":(1,0,0,0.25),"alpha":None}) 

一個完整的例子:

import seaborn as sns 
import matplotlib.pyplot as plt 
import numpy as np 

x = np.random.randn(60) 

ax = sns.distplot(x, label="BsrDI", bins=np.linspace(-3,3,10), kde=False, 
        hist_kws={"lw":3, "edgecolor": (1,0,0,0.75), 
            "color":(1,0,0,0.25),"alpha":None}) 

plt.show() 

enter image description here