Python 关于Seaborn子地块的GridSpec
我目前有2个子地块使用seaborn:Python 关于Seaborn子地块的GridSpec,python,matplotlib,seaborn,subplot,Python,Matplotlib,Seaborn,Subplot,我目前有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)
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()文档,但不知道如何将其应用于seaborn对象
问题:
谢谢您的时间。正如@dnalow提到的,
seaborn
对GridSpec
没有影响,因为您将对轴的引用传递给函数。像这样:
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()
正如@dnalow所提到的,seaborn
对GridSpec
没有影响,因为您将对轴的引用传递给函数。像这样:
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()
为什么seaborn会导致gridspec出现问题?您还可以从gridspec或subplot2grid获取轴对象。关于第二个问题:您是否尝试过ax1.xaxis.set_label_position(“top”)
?谢谢您的建议!我最终使用了fig.suptitle('bold fig SUPTTILE',fontsize=14,fontweight='bold')为什么seaborn会导致gridspec出现问题?您还可以从gridspec或subplot2grid获取轴对象。关于第二个问题:您是否尝试过ax1.xaxis.set_label_position(“top”)
?谢谢您的建议!最后我使用了fig.suptitle('bold fig SUPTTILE',fontsize=14,fontweight='bold'),谢谢!我经历了一段艰难的时光,因为我不知道如何翻译你从f,(ax1,ax2)=plt.subplot(2,sharex=True)中得到的内容。我编辑了答案,以展示如何使用plt.subplot
命令设置高度比,并避免使用更为冗长的GridSpec
布局。@mwaskom不错,我不知道这种语法。谢谢!我经历了一段艰难的时光,因为我不知道如何翻译你从f(ax1,ax2)=plt.subplot(2,sharex=True)中得到的信息。我编辑了答案,以展示如何使用plt.subplot
命令设置高度比,并避免使用更为冗长的GridSpec
布局。@mwaskom nice,我不知道这种语法。