简体   繁体   English

如何在 matplotlib 中制作空白子图?

[英]How can I make a blank subplot in matplotlib?

I am making a group of subplot (say, 3 x 2) in matplotlib, but I have fewer than 6 datasets.我正在 matplotlib 中制作一组子图(比如 3 x 2),但我的数据集少于 6 个。 How can I make the remaining subplot blank?我怎样才能让剩下的子图空白?

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.这可能 go 在几页上,但在最后一页上,有,例如,5 个数据集到 2,1 框将是空的。 However, I have declared the figure as:但是,我已将数字声明为:

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.因此,在子图 2,1 的空间中,有一组默认的带有刻度和标签的轴。 How can I programatically render that space blank and devoid of axes?我如何以编程方式将该空间渲染为空白且没有轴?

You could always hide the axes which you do not need.你总是可以隐藏你不需要的轴。 For example, the following code turns off the 6th 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:结果如下图:

3x2 图形网格的图像,右下单元格中没有呈现图形

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 (eg the tick marks and labels).或者,请参阅matplotlib 图中隐藏轴文本问题的已接受答案,了解保留轴但隐藏所有轴装饰(例如刻度线和标签)的方法。

A much improved subplot interface has been added to matplotlib since this question was first asked.自首次提出此问题以来,已向 matplotlib 添加了一个大大改进的子图界面 Here you can create exactly the subplots you need without hiding the extras.在这里,您可以准确地创建您需要的子图,而无需隐藏额外内容。 In addition, the subplots can span additional rows or columns.此外,子图可以跨越额外的行或列。

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()

在此处输入图像描述

It's also possible to hide a subplot using the Axes.set_visible() method.也可以使用 Axes.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)

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")

To delete the the plot positioned at (2,1) you may use要删除位于 (2,1) 的 plot,您可以使用

import matplotlib.pyplot as plt
cfig,ax = plt.subplots(3,2)
cfig.delaxes(ax.flatten()[5])

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

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