简体   繁体   English

删除matplotlib图中的空子图

[英]Remove empty sub plots in matplotlib figure

How can I determine whether a subplot ( AxesSubplot ) is empty or not? 如何确定子图( AxesSubplot )是否为空? I would like to deactivate empty axes of empty subplots and remove completely empty rows. 我想停用空子图的空轴并删除完全空行。

For instance, in this figure only two subplots are filled and the remaining subplots are empty. 例如,在该图中,仅填充了两个子图,其余的子图是空的。

import matplotlib.pyplot as plt

# create figure wit 3 rows and 7 cols; don't squeeze is it one list
fig, axes = plt.subplots(3, 7, squeeze=False)
x = [1,2]
y = [3,4]

# plot stuff only in two SubAxes; other axes are empty
axes[0][1].plot(x, y)
axes[1][2].plot(x, y)

# save figure
plt.savefig('image.png')

Note: It is mandatory to set squeeze to False . 注意:必须将squeeze设置为False

Basically I want a sparse figure. 基本上我想要一个稀疏的人物。 Some subplots in rows can be empty, but they should be deactivated (no axes must be visible). 行中的某些子图可以为空,但应将其取消激活(不能显示任何轴)。 Completely empty rows must be removed and must not be set to invisible. 必须删除完全空行,不得将其设置为不可见。

You can use the fig.delaxes() method: 您可以使用fig.delaxes()方法:

import matplotlib.pyplot as plt

# create figure wit 3 rows and 7 cols; don't squeeze is it one list
fig, axes = plt.subplots(3, 7, squeeze=False)
x = [1,2]
y = [3,4]

# plot stuff only in two SubAxes; other axes are empty
axes[0][1].plot(x, y)
axes[1][2].plot(x, y)

# delete empty axes
for i in [0, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17,
          18, 19, 20]:
    fig.delaxes(axes.flatten()[i])

# save figure
plt.savefig('image.png')
plt.show(block=False)

One way of achieving what you require is to use matplotlibs subplot2grid feature. 实现所需要的一种方法是使用matplotlibs subplot2grid功能。 Using this you can set the total size of the grid (3,7 in your case) and choose to only plot data in certain subplots in this grid. 使用此选项可以设置网格的总大小(在您的情况下为3,7),并选择仅绘制此网格中某些子图中的数据。 I have adapted your code below to give an example: 我在下面修改了你的代码,举个例子:

import matplotlib.pyplot as plt

x = [1,2]
y = [3,4]

fig = plt.subplots(squeeze=False)
ax1 = plt.subplot2grid((3, 7), (0, 1))
ax2 = plt.subplot2grid((3, 7), (1, 2))

ax1.plot(x,y)
ax2.plot(x,y)

plt.show()

This gives the following graph: 这给出了以下图表:

在此输入图像描述

EDIT: 编辑:

Subplot2grid, in effect, does give you a list of axes. Subplot2grid实际上会为您提供一个轴列表。 In your original question you use fig, axes = plt.subplots(3, 7, squeeze=False) and then use axes[0][1].plot(x, y) to specifiy which subplot your data will be plotted in. That is the same as what subplot2grid does, apart from it only shows the subplots with data in them which you have defined. 在您的原始问题中,您使用fig, axes = plt.subplots(3, 7, squeeze=False)然后使用axes[0][1].plot(x, y)来指定您的数据将被绘制在哪个子图中。这与subplot2grid的作用相同,除了它只显示您已定义的数据的子图。

So take ax1 = plt.subplot2grid((3, 7), (0, 1)) in my answer above, here I have specified the shape of the 'grid' which is 3 by 7. That means I can have 21 subplots in that grid if I wanted, exactly like you original code. 所以在上面的答案中取ax1 = plt.subplot2grid((3, 7), (0, 1)) ,这里我指定了'grid'的形状,它是3乘7。这意味着我可以有21个子图如果我想要那个网格,就像你的原始代码一样。 The difference is that your code displays all the subplots whereas subplot2grid does not. 不同之处在于您的代码显示所有子图,而subplot2grid则不显示。 The (3,7) in ax1 = ... above specifies the shape of the whole grid and the (0,1) specifies where in that grid the subplot will be shown. (3,7)ax1 = ...上面指定整个网格和形状(0,1)指定在哪里该网格的副区将被显示。

You can use any position the subplot wherever you like within that 3x7 grid. 您可以在3x7网格中的任何位置使用子图的任何位置。 You can also fill all 21 spaces of that grid with subplots that have data in them if you require by going all the way up to ax21 = plt.subplot2grid(...) . 如果您需要一直向上到ax21 = plt.subplot2grid(...)您还可以使用包含数据的子图填充该网格的所有21个空格。

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

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