gpt4 book ai didi

python - Seaborn 子图上的 GridSpec

转载 作者:行者123 更新时间:2023-12-02 15:16:28 26 4
gpt4 key购买 nike

我目前有 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()

这是输出:

Distribution Plot

我对如何使 ax2(底部图)相对于 ax1(顶部图)变得更短感到很困惑。我正在查看 GridSpec ( http://matplotlib.org/users/gridspec.html) 文档,但我无法弄清楚如何将它应用于 seaborn 对象。

问题:

  1. 如何让底部的子图比顶部的更短次要情节?
  2. 顺便说一句,我如何将情节的标题“差异分布”移至顶部次要情节?

感谢您的宝贵时间。

最佳答案

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

enter image description here

关于python - Seaborn 子图上的 GridSpec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40070093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com