在同一坐标轴上绘制两个seaborn distplot图形

8
我正在尝试找到一种漂亮的方式在同一轴上绘制两个distplots(来自seaborn)。它并不像我想象中那样漂亮,因为直方图条形图会相互覆盖。而且我不想使用countplotbarplot,只是因为它们看起来不够漂亮。如果没有其他方法,我当然会这样做,但是distplot看起来非常好。但是,如上所述,条形图现在彼此重叠(见图片)。

因此,有没有办法将两个distplot频率条形图适配到一个bin上,以便它们不重叠?或者将计数放置在彼此顶部?基本上,我想在seaborn中做到这一点:

enter image description here

任何清理它的想法都非常欢迎。谢谢。
MWE:
sns.set_context("paper",font_scale=2)
sns.set_style("white")
rc('text', usetex=False)
fig, ax = plt.subplots(figsize=(7,7),sharey=True)
sns.despine(left=True)

mats=dict()
mats[0]=[1,1,1,1,1,2,3,3,2,3,3,3,3,3]
mats[1]=[3,3,3,3,3,4,4,4,5,6,1,1,2,3,4,5,5,5]
N=max(max(set(mats[0])),max(set(mats[1])))

binsize = np.arange(0,N+1,1)
B=['Thing1','Thing2']
for i in range(len(B)):
    ax = sns.distplot(mats[i],
                      kde=False,
                      label=B[i],
                      bins=binsize)

ax.set_xlabel('My label')
ax.get_yaxis().set_visible(False)
ax.legend()
plt.show()

enter image description here


你是在询问如何绘制堆积直方图吗? - mwaskom
不是我在问如何在你提供的链接中实现右下角或左上角的效果,而是我在问如何在seaborn中实现这个效果。 - Astrid
3
Seaborn使用matplotlib。没有“在seaborn中”。 - mwaskom
1
非常正确,但是seaborn有它自己的函数,比如上面那个。我宁愿让它起作用,而不是退回到matplotlib上。 - Astrid
1
请参见此链接 - borgr
1个回答

7

正如@mwaskom所说,seaborn在很大程度上包装了matplotlib绘图函数,以提供更复杂和更美观的图表。

你要寻找的内容 "simple enough" 可以使用matplotlib来完成:

sns.set_context("paper", font_scale=2)
sns.set_style("white")
plt.rc('text', usetex=False)
fig, ax = plt.subplots(figsize=(4,4))
sns.despine(left=True)

# mats=dict()
mats0=[1,1,1,1,1,2,3,3,2,3,3,3,3,3]
mats1=[3,3,3,3,3,4,4,4,5,6,1,1,2,3,4,5,5,5]
N=max(mats0 + mats1)

# binsize = np.arange(0,N+1,1)
binsize = N
B=['Thing1','Thing2']

ax.hist([mats0, mats1], binsize, histtype='bar', 
        align='mid', label=B, alpha=0.4)#, rwidth=0.6)

ax.set_xlabel('My label')
ax.get_yaxis().set_visible(False)
# ax.set_xlim(0,N+1)
ax.legend()
plt.show()

这将产生:

enter image description here

您可以取消注释ax.set_xlim(0,N+1),以在此直方图周围提供更多空间。


噢,我明白了,那看起来非常相似。注意,我并不怀疑@mwascom,他自然是正确的。我在想是否可能以更“seaborn”的方式(是的,我自己也不确定这是什么意思)来完成上述操作。无论如何,谢谢你们两个。 - Astrid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接