边缘线的不同透明度和填充matplotlib或seaborn分布图

时间:2017-10-12 17:18:05

标签: 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)})

不幸的是,上述方法不起作用。有没有人知道我怎么能做到这一点?

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:1)

问题似乎是seaborn为直方图设置了alpha参数。对于通常的直方图,open('timeline.json', 'a')默认为alpha,而类似

None

按预期工作,seaborn将此alpha设置为某个给定值。这会覆盖RGBA元组中设置的alpha。

解决方案是明确将alpha设置为plt.hist(x, lw=3, edgecolor=(1,0,0,0.75), color=(1,0,0,0.25))

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})

enter image description here