如何在matplotlib中创建一个空白的子情节?

时间:2022-09-15 23:43:05

I am making a group of subplot (say, 3 x 2) in matplotlib, but I have fewer than 6 datasets. How can I make the remaining subplot blank?

我在matplotlib中创建了一组子情节(比如,3x2),但是我只有不到6个数据集。我怎样才能使剩下的子情节空白?

The arrangement looks like this:

安排如下:

+----+----+
| 0,0| 0,1|
+----+----+
| 1,0| 1,1|
+----+----+
| 2,0| 2,1|
+----+----+

This may go on for several pages, but on the final page, there are, for example, 5 datasets to the 2,1 box will be empty. However, I have declared the figure as:

这可能会持续几页,但在最后一页,例如,有5个数据集到2,1个方框将是空的。但是,我已经宣布这个数字为:

cfig,ax = plt.subplots(3,2)

So in the space for subplot 2,1 there is a default set of axes with ticks and labels. How can I programatically render that space blank and devoid of axes?

在子图2 1的空间中有一组带有刻度和标签的默认坐标轴。如何以编程的方式将该空间呈现为空且没有坐标轴?

4 个解决方案

#1


82  

You could always hide the axes which you do not need. For example, the following code turns of the 6-th axes completely:

你可以隐藏不需要的坐标轴。例如,下面的代码完全旋转了6个坐标轴:

import matplotlib.pyplot as plt

hf, ha = plt.subplots(3,2)
ha[-1, -1].axis('off')

plt.show()

and results in the following figure:

结果如下图所示:

如何在matplotlib中创建一个空白的子情节?

Alternatively, see the accepted answer to the question Hiding axis text in matplotlib plots for a way of keeping the axes but hiding all the axes decorations (e.g. the tick marks and labels).

另一种方法是,在matplotlib图中,查看被接受的答案,隐藏轴文本,以保持坐标轴,但隐藏所有的轴装饰(例如,标记和标签)。

#2


17  

A much improved subplot interface has been added to matplotlib since this question was first asked. Here you can create exactly the subplots you need without hiding the extras. In addition, the subplots can span additional rows or columns.

自从第一次提出这个问题以来,matplotlib已经添加了一个改进的子情节接口。在这里,您可以创建所需的子情节,而不必隐藏额外的内容。此外,子块可以跨越额外的行或列。

import pylab as plt

ax1 = plt.subplot2grid((3,2),(0, 0))
ax2 = plt.subplot2grid((3,2),(0, 1))
ax3 = plt.subplot2grid((3,2),(1, 0))
ax4 = plt.subplot2grid((3,2),(1, 1))
ax5 = plt.subplot2grid((3,2),(2, 0))

plt.show()

如何在matplotlib中创建一个空白的子情节?

#3


1  

Would it be an option to create the subplots when you need them?

是否可以在需要的时候创建子图?

import matplotlib
matplotlib.use("pdf")
import matplotlib.pyplot as plt

plt.figure()
plt.gcf().add_subplot(421)
plt.fill([0,0,1,1],[0,1,1,0])
plt.gcf().add_subplot(422)
plt.fill([0,0,1,1],[0,1,1,0])
plt.gcf().add_subplot(423)
plt.fill([0,0,1,1],[0,1,1,0])
plt.suptitle("Figure Title")
plt.gcf().subplots_adjust(hspace=0.5,wspace=0.5)
plt.savefig("outfig")

#4


1  

It's also possible to hide a subplot using the Axes.set_visible() method.

也可以使用ax .set_visible()方法隐藏子情节。

import matplotlib.pyplot as plt
import pandas as pd

fig = plt.figure()
data = pd.read_csv('sampledata.csv')

for i in range(0,6):
ax = fig.add_subplot(3,2,i+1)
ax.plot(range(1,6), data[i])
if i == 5:
    ax.set_visible(False)

#1


82  

You could always hide the axes which you do not need. For example, the following code turns of the 6-th axes completely:

你可以隐藏不需要的坐标轴。例如,下面的代码完全旋转了6个坐标轴:

import matplotlib.pyplot as plt

hf, ha = plt.subplots(3,2)
ha[-1, -1].axis('off')

plt.show()

and results in the following figure:

结果如下图所示:

如何在matplotlib中创建一个空白的子情节?

Alternatively, see the accepted answer to the question Hiding axis text in matplotlib plots for a way of keeping the axes but hiding all the axes decorations (e.g. the tick marks and labels).

另一种方法是,在matplotlib图中,查看被接受的答案,隐藏轴文本,以保持坐标轴,但隐藏所有的轴装饰(例如,标记和标签)。

#2


17  

A much improved subplot interface has been added to matplotlib since this question was first asked. Here you can create exactly the subplots you need without hiding the extras. In addition, the subplots can span additional rows or columns.

自从第一次提出这个问题以来,matplotlib已经添加了一个改进的子情节接口。在这里,您可以创建所需的子情节,而不必隐藏额外的内容。此外,子块可以跨越额外的行或列。

import pylab as plt

ax1 = plt.subplot2grid((3,2),(0, 0))
ax2 = plt.subplot2grid((3,2),(0, 1))
ax3 = plt.subplot2grid((3,2),(1, 0))
ax4 = plt.subplot2grid((3,2),(1, 1))
ax5 = plt.subplot2grid((3,2),(2, 0))

plt.show()

如何在matplotlib中创建一个空白的子情节?

#3


1  

Would it be an option to create the subplots when you need them?

是否可以在需要的时候创建子图?

import matplotlib
matplotlib.use("pdf")
import matplotlib.pyplot as plt

plt.figure()
plt.gcf().add_subplot(421)
plt.fill([0,0,1,1],[0,1,1,0])
plt.gcf().add_subplot(422)
plt.fill([0,0,1,1],[0,1,1,0])
plt.gcf().add_subplot(423)
plt.fill([0,0,1,1],[0,1,1,0])
plt.suptitle("Figure Title")
plt.gcf().subplots_adjust(hspace=0.5,wspace=0.5)
plt.savefig("outfig")

#4


1  

It's also possible to hide a subplot using the Axes.set_visible() method.

也可以使用ax .set_visible()方法隐藏子情节。

import matplotlib.pyplot as plt
import pandas as pd

fig = plt.figure()
data = pd.read_csv('sampledata.csv')

for i in range(0,6):
ax = fig.add_subplot(3,2,i+1)
ax.plot(range(1,6), data[i])
if i == 5:
    ax.set_visible(False)