Seaborn 子图上的 GridSpec

joh*_*n05 2 python matplotlib subplot seaborn

我目前有 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()
Run Code Online (Sandbox Code Playgroud)

这是输出:

分布图

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

题:

  1. 与顶部子图相比,如何使底部子图更短?
  2. 顺便说一句,我如何将情节的标题“Distrubition of Difference”移到顶部子情节之上?

感谢您的时间。

Diz*_*ahi 7

正如@dnalow 提到的,seaborn对 没有影响GridSpec,因为您将对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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明