2017-02-19 70 views
3

我已經作圖4個棒形圖表示4'S,6的,2'S,1的通過使用下面的代碼擊球手得分最高編號:我如何在matplotlib中的單個矩形網格中繪製多個圖?

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)的網格中?

回答

1

使用pyplot.subplots。在下面的例子中,我使用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()