Pandas/Matplotlib 直方图问题 - x 轴刻度在子图和额外不需要的图表中消失 space。尝试了 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 中绘制直方图子图时遇到了几个问题(这似乎是直方图特有的;条形图和折线图似乎没有这个问题):
我无法让 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 轴刻度值。
- 子图似乎在右侧的一些图表中留下了额外的 space。我如何摆脱 space 并将整个 x 轴用于我的直方图?
这是整个事情的原样。
为什么有些 x 轴刻度会自动显示,有些则不会?为什么我的图表有这么多额外的 space?当我制作条形图而不是直方图时,这不是问题...(请注意,我也尝试在 subplot_adjust
中调整 hspace
和 wspace
)。
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()
这是 pandas 0.16.0 中出现的一个有趣的效果。升级到 0.16.1 修复它。
我在 Matplotlib 中绘制直方图子图时遇到了几个问题(这似乎是直方图特有的;条形图和折线图似乎没有这个问题):
我无法让 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 轴刻度值。
- 子图似乎在右侧的一些图表中留下了额外的 space。我如何摆脱 space 并将整个 x 轴用于我的直方图?
这是整个事情的原样。
为什么有些 x 轴刻度会自动显示,有些则不会?为什么我的图表有这么多额外的 space?当我制作条形图而不是直方图时,这不是问题...(请注意,我也尝试在 subplot_adjust
中调整 hspace
和 wspace
)。
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()
这是 pandas 0.16.0 中出现的一个有趣的效果。升级到 0.16.1 修复它。