【发布时间】:2015-08-18 13:42:31
【问题描述】:
我在 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 轴刻度值。
- 子图似乎在右侧的某些图表中留下了额外的空间。如何摆脱该空间并将整个 x 轴用作直方图?
这就是整个事情的样子。
为什么有些 x 轴刻度会自动显示,而有些则不会?为什么我的图表有这么多额外空间?当我制作条形图而不是直方图时,这不是问题...(请注意,我也尝试在 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()
【问题讨论】:
-
这是否为您解答(
xlabel()和ylabel())? stackoverflow.com/a/12444777/1716866 -
x 轴标签是什么意思?你是说xlabel吗? ...我没有看到任何...您是指 xticklabels 的大小吗?还是不同图的 x 轴范围不一致?请更具体!
-
我同意@plonser:你的问题不清楚。在您发布的图片中,所有字体大小似乎都是一致的。
-
@plonser - 调整了我的问题,有什么想法吗?提前致谢
-
当我使用
plt.plot(...)而不是firstperiod.megaball.plot它对我有用时,xticklabels 会按预期显示。那么,您的成员函数plot有什么奇怪的地方吗?
标签: python matplotlib histogram font-size