【问题标题】:Two seaborn distplots one same axis两个 seaborn distplots 一个相同的轴
【发布时间】:2016-05-07 11:27:40
【问题描述】:

我试图找到一种在同一轴上绘制两个distplots (from 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()

【问题讨论】:

  • 你是在问如何画stacked histogram
  • 不,我问的是如何在您的链接中进行右下角或左上角。我在问如何在 seaborn 中做到这一点。
  • Seaborn 使用 matplotlib。没有“海生”。
  • 非常正确,但是 seaborn 确实有自己的功能,就像上面的那个。我宁愿在回到 matplotlib 之前完成这项工作。
  • 另见this

标签: python matplotlib seaborn


【解决方案1】:

正如@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()

产量:

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

【讨论】:

  • 啊,我明白了,看起来确实很相似。请注意,我并没有怀疑@mwaskom,他自然是正确的。我想知道是否有可能以更“海生”的方式进行上述操作(是的,我不确定自己是什么意思)。无论如何,谢谢你们。
猜你喜欢
  • 2019-03-10
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
相关资源
最近更新 更多