Seaborn 子图上的 GridSpec

GridSpec on Seaborn Subplots

我目前有 2 个使用 seaborn 的子图:

import matplotlib.pyplot as plt
import seaborn.apionly as sns

f, (ax1, ax2) = plt.subplots(2, sharex=True)

sns.distplot(df['Difference'].values, ax=ax1) #array, top subplot

sns.boxplot(df['Difference'].values, ax=ax2, width=.4) #bottom subplot
sns.stripplot([cimin, cimax], color='r', marker='d') #overlay confidence intervals over boxplot

ax1.set_ylabel('Relative Frequency') #label only the top subplot

plt.xlabel('Difference')

plt.show()

这是输出:

我对如何使 ax2(底部图)相对于 ax1(顶部图)变短感到很困惑。我正在查看 GridSpec (http://matplotlib.org/users/gridspec.html) 文档,但我不知道如何将它应用于 seaborn objects。

问题:

  1. 如何使底部子图比顶部短 次要情节?
  2. 顺便提一下,如何将情节的标题 "Distrubition of Difference" 移动到顶部以上 次要情节?

感谢您的宝贵时间。

如@dnalow 所述,seabornGridSpec 没有影响,因为您将对 Axes 对象的引用传递给函数。像这样:

import matplotlib.pyplot as plt
import seaborn.apionly as sns
import matplotlib.gridspec as gridspec

tips = sns.load_dataset("tips")

gridkw = dict(height_ratios=[5, 1])
fig, (ax1, ax2) = plt.subplots(2, 1, gridspec_kw=gridkw)

sns.distplot(tips.loc[:,'total_bill'], ax=ax1) #array, top subplot
sns.boxplot(tips.loc[:,'total_bill'], ax=ax2, width=.4) #bottom subplot

plt.show()

如果您使用的是 FacetGrid (either directly or through something like catplot,间接使用它),那么您可以传递 gridspec_kws.

这是一个使用 catplot 的示例,其中“var3”有两个值,即有两个子图,我以 3:8 的比例显示,un-shared x-axes.

g = sns.catplot(data=data, x="bin", y="y", col="var3", hue="var4", kind="bar", 
                sharex=False,
                facet_kws={
                  'gridspec_kws': {'width_ratios': [3, 8]}
                })

# Make the first subplot have a custom `xlim`:
g.axes[0][0].set_xlim(right=2.5)

结果,隐藏了标签,因为我只是复制了实际数据的输出,所以标签没有意​​义。