删除matplotlib子图中的多余图 [英] Remove the extra plot in the matplotlib subplot
问题描述
我想在 2 x 3 设置(即 2 行和 3 列)中绘制 5 个数据框.这是我的代码:但是在第 6 个位置(第二行和第三列)有一个额外的空图,我想摆脱它.我想知道如何删除它,以便在第一行中有三个图,第二行中有两个图.
I want to plot 5 data frames in a 2 by 3 setting (i.e. 2 rows and 3 columns). This is my code: However there is an extra empty plot in the 6th position (second row and third column) which I want to get rid of it. I am wondering how I could remove it so that I have three plots in the first row and two plots in the second row.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=3)
fig.set_figheight(8)
fig.set_figwidth(15)
df[2].plot(kind='bar',ax=axes[0,0]); axes[0,0].set_title('2')
df[4].plot(kind='bar',ax=axes[0,1]); axes[0,1].set_title('4')
df[6].plot(kind='bar',ax=axes[0,2]); axes[0,2].set_title('6')
df[8].plot(kind='bar',ax=axes[1,0]); axes[1,0].set_title('8')
df[10].plot(kind='bar',ax=axes[1,1]); axes[1,1].set_title('10')
plt.setp(axes, xticks=np.arange(len(observations)), xticklabels=map(str,observations),
yticks=[0,1])
fig.tight_layout()
推荐答案
尝试一下:
fig.delaxes(axes[1][2])
创建子图的一种更加灵活的方法是 fig.add_axes()
方法.参数是一个矩形坐标列表:fig.add_axes([x, y, xsize, ysize])
.这些值与画布大小有关,因此 0.5
的 xsize
表示子图具有窗口宽度的一半.
A much more flexible way to create subplots is the fig.add_axes()
method. The parameters is a list of rect coordinates: fig.add_axes([x, y, xsize, ysize])
. The values are relative to the canvas size, so an xsize
of 0.5
means the subplot has half the width of the window.
这篇关于删除matplotlib子图中的多余图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!