簡體   English   中英

Pandas / Matplotlib直方圖問題 - x軸刻度消失了子圖和額外不需要的圖表空間。 嘗試了tight_layout和subplots_adjust

[英]Pandas/Matplotlib histogram issues - x-axis ticks disappearing w subplots and extra unneeded chart space. Tried tight_layout and subplots_adjust

我有幾個問題在Matplotlib中繪制直方圖子圖(這似乎是直方圖特定的;相同的條形圖和折線圖似乎似乎沒有這個問題):

  1. 我不能讓x軸刻度標簽顯示在我的前2個圖表上。 在下面的代碼中,我沒有指定子圖分享x軸或任何東西。 我也嘗試使用plt.setp(ax.get_xticklabels(), visible=True) ,但它不會改變缺少x軸標簽的位數。

     plt.subplot2grid((6,4), [0,0], 2, 2) ax = firstperiod.megaball.plot(kind='hist', bins = 25) plt.setp(ax.get_xticklabels(), visible=True) plt.xticks(range(0,26,5), range(0,26,5), rotation="horizontal") plt.title('Megaball Distrib \\'96 - \\'99') plt.ylabel("# of draws", fontsize = 10) 

我注意到如果我只繪制左上方的直方圖,x軸刻度實際上會顯示出來,但一旦我繪制更多就會消失。

在此輸入圖像描述

我也試過調整tight_layout, plt.tight_layout(w_pad = 2, h_pad = 2) ,但這無助於顯示我的x軸刻度值。

  1. subplot似乎在右側的某些圖表中留下了額外的空間。 如何擺脫該空間並使用整個x軸作為直方圖?

這就是整個事情的樣子。

在此輸入圖像描述

為什么有些x軸刻度會自動顯示,有些則沒有? 為什么我的圖表有這么多額外空間? 當我制作條形圖而不是直方圖時,這不是問題...(請注意,我已經嘗試在subplot_adjust調整hspacewspace )。

fig = plt.figure()
fig.suptitle('Distribution of MegaBall Draws', fontsize=20)

plt.subplot2grid((6,4), [0,0], 2, 2)
ax = firstperiod.megaball.plot(kind='hist', bins = 25)
plt.setp(ax.get_xticklabels(), visible=True) 
plt.xticks(range(0,26,5), range(0,26,5), rotation="horizontal")
plt.title('Megaball Distrib \'96 - \'99')
plt.ylabel("# of draws", fontsize = 10)

plt.subplot2grid((6,4), [0,2], 2, 2)
secondperiod.megaball.plot(kind='hist', bins = 36)
plt.xticks(range(0,36,5), range(0,41,5), rotation="horizontal")
plt.title('Megaball Distrib \'99 - \'02')
plt.ylabel("# of draws", fontsize = 10)

plt.subplot2grid((6,4), [2,0], 2, 2)
thirdperiod.megaball.plot(kind='hist', bins = 52)
plt.xticks(range(0,55,5), range(0,55,5), rotation="horizontal")
plt.title('Megaball Distrib \'02 - \'05')
plt.ylabel("# of draws", fontsize = 10)

plt.subplot2grid((6,4), [2,2], 2, 2)
fourthperiod.megaball.plot(kind='hist', bins = 46)
plt.xticks(range(0,50,5), range(0,50,5),rotation="horizontal")
plt.title('Megaball Distrib \'05 - \'13')
plt.ylabel("# of draws", fontsize = 10)

plt.subplot2grid((6,4), [4,1], 2, 2)
fifthperiod.megaball.plot(kind='hist', bins = 15)
plt.xticks(rotation="horizontal")
plt.title('Megaball Distrib \'13 - ')
plt.ylabel("# of draws", fontsize = 10)

plt.tight_layout(w_pad = 2, h_pad = 2)
plt.subplots_adjust(top = 0.8, wspace = 0.75, hspace = 2.5)
plt.savefig("megaball_distribs.png")
plt.show()

這是一個有趣的效果,似乎出現在熊貓0.16.0中。 升級到0.16.1修復了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM