两个seaborn distplots同一轴

Ast*_*rid 6 python matplotlib seaborn

我试图找到一个很好的方法来绘制同一轴上的两个distplots(来自seaborn).它不像我想要的那样漂亮,因为直方图条是相互覆盖的.我不想使用countplotbarplot只是因为它们看起来不漂亮.当然,如果没有其他方式我会以这种方式做,但distplot看起来非常好.但是,如上所述,这些酒吧现在相互覆盖(见图).

因此,有没有办法将两个distplot频率条拟合到一个bin上,以便它们不重叠?或者把计数放在彼此之上?基本上我想在seaborn中这样做:

在此输入图像描述

任何清理它的想法都是最受欢迎的.谢谢.

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Pri*_*mer 6

正如@mwaskom 所说,seaborn 正在包装 matplotlib 绘图函数(大部分情况下)以提供更复杂、更美观的图表。

您正在寻找的是“足够简单”以使用 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()
Run Code Online (Sandbox Code Playgroud)

其中产生:

在此处输入图片说明

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