在 Python 中具有共享轴的 GridSpec

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22511550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:05:07  来源:igfitidea点击:

GridSpec with shared axes in Python

pythonmatplotlib

提问by Amelio Vazquez-Reina

This solutionto another thread suggests using gridspec.GridSpecinstead of plt.subplots. However, when I share axes between subplots, I usually use a syntax like the following

另一个线程的此解决方案建议使用gridspec.GridSpec而不是plt.subplots. 但是,当我在子图之间共享轴时,我通常使用如下语法

  fig, axes = plt.subplots(N, 1, sharex='col', sharey=True, figsize=(3,18))

How can I specify sharexand shareywhen I use GridSpec?

如何指定sharex以及sharey何时使用GridSpec

采纳答案by Joe Kington

First off, there's an easier workaround for your original problem, as long as you're okay with being slightly imprecise. Just reset the top extent of the subplots to the default aftercalling tight_layout:

首先,对于您的原始问题,有一个更简单的解决方法,只要您可以稍微不精确。只需调用后将子图的顶部范围重置为默认值tight_layout

fig, axes = plt.subplots(ncols=2, sharey=True)
plt.setp(axes, title='Test')
fig.suptitle('An overall title', size=20)

fig.tight_layout()
fig.subplots_adjust(top=0.9) 

plt.show()

enter image description here

在此处输入图片说明



However, to answer your question, you'll need to create the subplots at a slightly lower level to use gridspec. If you want to replicate the hiding of shared axes like subplotsdoes, you'll need to do that manually, by using the shareyargument to Figure.add_subplotand hiding the duplicated ticks with plt.setp(ax.get_yticklabels(), visible=False).

但是,要回答您的问题,您需要在稍低的级别创建子图才能使用 gridspec。如果您想像subplotsdo一样复制共享轴的隐藏,您需要手动执行此操作,方法是使用sharey参数 toFigure.add_subplot并使用plt.setp(ax.get_yticklabels(), visible=False).

As an example:

举个例子:

import matplotlib.pyplot as plt
from matplotlib import gridspec

fig = plt.figure()
gs = gridspec.GridSpec(1,2)
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1], sharey=ax1)
plt.setp(ax2.get_yticklabels(), visible=False)

plt.setp([ax1, ax2], title='Test')
fig.suptitle('An overall title', size=20)
gs.tight_layout(fig, rect=[0, 0, 1, 0.97])

plt.show()

enter image description here

在此处输入图片说明

回答by khyox

Both Joe's choices gave me some problems: the former, related with direct use of figure.tight_layoutinstead of figure.set_tight_layout()and, the latter, with some backends (UserWarning: tight_layout : falling back to Agg renderer). But Joe's answer definitely cleared my way toward another compact alternative. This is the result for a problem close to the OP's one:

Joe 的两个选择都给我带来了一些问题:前者与直接使用figure.tight_layout而不是有关figure.set_tight_layout(),后者与一些后端有关(UserWarning:tight_layout :回退到 Agg 渲染器)。但乔的回答无疑为我走向另一个紧凑的替代方案扫清了道路。这是接近 OP 的问题的结果:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=1, sharex='col', sharey=True,
                               gridspec_kw={'height_ratios': [2, 1]},
                               figsize=(4, 7))
fig.set_tight_layout({'rect': [0, 0, 1, 0.95], 'pad': 1.5, 'h_pad': 1.5})
plt.setp(axes, title='Test')
fig.suptitle('An overall title', size=20)

plt.show()

enter image description here

在此处输入图片说明