简体   繁体   English

从 Matplotlib 图中删除子图的列/行

[英]remove column/row of subplots from Matplotlib figure

I have a library function which generates a figure with a 5x5 grid of subplots, using pyplot.我有一个库函数,它使用 pyplot 生成一个带有 5x5 子图网格的图形。 In some instances, I'd like to remove some of the columns or lines to give more space to whichever subplots are more rlevant in some particular scenario.在某些情况下,我想删除一些列或行,以便为在某些特定情况下更相关的子图提供更多空间。

I've found that I can delete subplots after the fact, using fig.delaxes(ax) .我发现我可以在事后删除子图,使用fig.delaxes(ax) However, that just leaves an empty space where the subplot was, even if I delete a whole row or column of subplots this way, change the spacings between subplots, redraw or use fig.tight_layout() .但是,这只会在子图所在的位置留下一个空白空间,即使我以这种方式删除了一整行或一列子图,更改子图之间的间距,重绘或使用fig.tight_layout()

Is there a method to get Matplotlib to close those gaps?有没有办法让 Matplotlib 缩小这些差距? I could probably edit the code which generates the figures to skip the rows/columns in question, but I'd like to avoid that if possible.我可能可以编辑生成数字的代码以跳过有问题的行/列,但如果可能的话,我想避免这种情况。

Sure, you can set the height_ratios after the fact:当然,您可以在事后设置 height_ratios:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(3, 5, constrained_layout=True)

for i in range(5):
    axs[1, i].set_xticks([])
    axs[1, i].set_yticks([])

gs = axs[0, 0].get_subplotspec().get_gridspec()
gs.set_height_ratios([1, 0.0001, 1])
plt.tight_layout()

for ax in axs[1, :]:
    fig.delaxes(ax)

... which isn't perfect, but pretty close... ......这并不完美,但非常接近......

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM