【问题标题】:GridSpec on Seaborn SubplotsSeaborn 子图上的 GridSpec
【发布时间】:2017-02-25 11:24:42
【问题描述】:

我目前有 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 对象。

问题:

  1. 如何使底部子图比顶部短 子情节?
  2. 顺便说一句,我如何将情节的标题“差异分布”移动到顶部上方 子情节?

感谢您的宝贵时间。

【问题讨论】:

  • 为什么 seaborn 会导致 gridspec 出现问题?您还将从 gridspec 或 subplot2grid 获得轴对象。关于你的第二个问题:你试过ax1.xaxis.set_label_position("top") 吗?
  • 感谢您的建议!我最终使用了 fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')

标签: python matplotlib seaborn subplot


【解决方案1】:

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

【讨论】:

  • 谢谢!我很难过,因为我不知道如何翻译你从中得到的东西
  • f, (ax1, ax2) = plt.subplots(2, sharex=True)
  • 我编辑了答案,展示了如何使用plt.subplots 命令设置高度比,避免使用更冗长的GridSpec 布局。
  • @mwaskom 不错,我不知道那种语法。
猜你喜欢
  • 2021-09-29
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 2020-08-22
  • 2015-09-18
  • 2020-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多