Python 如何在matplotlib中在单个矩形栅格中绘制多个绘图?

Python 如何在matplotlib中在单个矩形栅格中绘制多个绘图?,python,pandas,matplotlib,plot,Python,Pandas,Matplotlib,Plot,我绘制了4个条形图,显示了击球手使用以下代码得分的4、6、2、1中的最高数: for i in [6,4,2,1]: ax=delivery[delivery['batsman_runs']==i].batsman.value_counts()[:10].plot.bar(width=0.8) for p in ax.patches: ax.annotate(format(p.get_height()), (p.get_x()+0.10, p.get_height()+1)) m

我绘制了4个条形图,显示了击球手使用以下代码得分的4、6、2、1中的最高数:

for i in [6,4,2,1]:
ax=delivery[delivery['batsman_runs']==i].batsman.value_counts()[:10].plot.bar(width=0.8)
for p in ax.patches:
        ax.annotate(format(p.get_height()), (p.get_x()+0.10, p.get_height()+1))
mlt.show()

现在,这种方法将条形图一个接一个地绘制出来。如何在(2x2)网格中并排绘制这些条形图?

您可以使用matplotlib的子图:

使用。在下面的示例中,我使用pyplot作为plt

fig, axes = plt.subplots((2,2))
arr = [6,4,2,1]
for i in range(len(arr)):
    if i  < 2:
        axes[0][i].bar(i, delivery[delivery['batsman_runs']==arr [i]].batsman.value_counts()[:10],  0.8)
    else:
        axes[1][i - 2].bar(i, delivery[delivery['batsman_runs']==arr [i]].batsman.value_counts()[:10], 0.8)
plt.show()
图,轴=plt.子图((2,2))
arr=[6,4,2,1]
对于范围内的i(len(arr)):
如果i<2:
轴[0][i].bar(i,传递[delivery['batsman_runs']==arr[i].batsman.value_counts()[:10],0.8)
其他:
轴[1][i-2].bar(i,传递[delivery['batsman_runs']==arr[i]].batsman.value_counts()[:10],0.8)
plt.show()