pandas Matplotlib 缺少 x 刻度标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42820496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib missing x tick labels
提问by econ99
Once I run the code below, the x tick labels for the third plot, ax3, does not show up. Yet, I only removed the x tick labels for ax1 and ax2. Any solution to having the dates appear on the x axis of my third plot, ax3?
运行下面的代码后,第三个图 ax3 的 x 刻度标签不会显示。然而,我只删除了 ax1 和 ax2 的 x 刻度标签。让日期出现在我的第三个图 ax3 的 x 轴上的任何解决方案?
plt.figure()
ax1 = plt.subplot2grid((8,1),(0,0), rowspan=4, colspan=1)
ax1.yaxis.set_major_locator(mticker.MaxNLocator(nbins=10, prune='lower'))
plt.setp(ax1.get_xticklabels(), visible=False)
ax2 = plt.subplot2grid((8,1),(4,0), rowspan=2, colspan=1, sharex = ax1)
plt.setp(ax2.get_xticklabels(), visible=False)
ax3 = plt.subplot2grid((8,1),(6,0), rowspan=2, colspan=1, sharex = ax1)
ax3.xaxis.set_major_locator(mticker.MaxNLocator(10))
ax3.xaxis.set_minor_locator(mticker.MaxNLocator(20))
'''
# This has been ***removed*** in corrected version
for label in ax3.xaxis.get_ticklabels():
label.set_rotation(45)
plt.xlabel('Dates') #This label does not appear in the figure either
'''
ax3.yaxis.set_major_locator(mticker.MaxNLocator(nbins=5, prune='upper'))
main.dropna(inplace=True)
main['sales1'].plot(ax=ax1)
main['sales2'].plot(ax=ax1)
cmain.plot(ax=ax2)
main[['rollsales1', 'rollsales2']].plot(ax=ax3)
'''
# This has been added to corrected version.
plt.setp(ax3.xaxis.get_label(), visible=True, text='Dates')
plt.setp(ax3.get_xticklabels(), visible=True, rotation=30, ha='right')
'''
plt.show()
回答by James
In matplotlib, using sharex
or sharey
turns off the ticklabels by default in version 2. So you can drop the pieces of code that set the label visibility to False
. Also, rather than iterating over each label to change the parameters, you can set the parameters for all of them in one shot using setp
.
在 matplotlib 中,在版本 2 中默认使用sharex
或sharey
关闭刻度标签。因此您可以删除将标签可见性设置为 的代码片段False
。此外,您无需遍历每个标签来更改参数,而是可以使用setp
.
I had to make fake data to simulate your graphs, so my data may look bizarre.
我不得不制作假数据来模拟你的图表,所以我的数据可能看起来很奇怪。
plt.figure()
ax1 = plt.subplot2grid((8,1),(0,0), rowspan=4, colspan=1)
ax2 = plt.subplot2grid((8,1),(4,0), rowspan=2, colspan=1, sharex=ax1)
ax3 = plt.subplot2grid((8,1),(6,0), rowspan=2, colspan=1, sharex=ax1)
ax1.yaxis.set_major_locator(mticker.MaxNLocator(nbins=10, prune='lower')
ax3.yaxis.set_major_locator(mticker.MaxNLocator(nbins=5, prune='upper'))
main.dropna(inplace=True)
main['sales1'].plot(ax=ax1)
main['sales2'].plot(ax=ax1)
cmain.plot(ax=ax2)
main[['rollsales1', 'rollsales2']].plot(ax=ax3)
# set the xaxis label
plt.setp(ax3.xaxis.get_label(), visible=True, text='Dates')
# set the ticks
plt.setp(ax3.get_xticklabels(), visible=True, rotation=30, ha='right')
# turn off minor ticks
plt.minorticks_off()
plt.show()