【问题标题】:Pandas/Matplotlib histogram issues - x-axis ticks disappearing w subplots and extra unneeded chart space. Tried tight_layout and subplots_adjustPandas/Matplotlib 直方图问题 - x 轴刻度消失 w 子图和额外不需要的图表空间。尝试了tight_layout 和subplots_adjust
【发布时间】:2015-08-19 18:09:39
【问题描述】:

我在 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. 子图似乎在右侧的某些图表中留下了额外的空间。如何摆脱该空间并将整个 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()

【问题讨论】:

  • 看起来您正在使用一种样式 - 当您使用 matplotlib 默认值时会发生什么?另外,尝试使用一些虚假数据进行复制,给我们一个MCVE。
  • @Ajean 你指的是什么风格?
  • 实际上,您似乎在使用 pandas - 这可以解释灰色背景和白色网格线(pandas 在绘制 DataFrame 时使用特定样式)。您还在使用 pyplot 的同时还通过每个 DataFrame(系列?)而不是直接使用 matplotlib 对象进行绘图。我想后者会更一致地工作。
  • @Ajean - 直接使用 matplotlib 对象的方法是什么?我只知道我有一些 Pandas 系列,我想在直方图中绘制图表。我认为熊猫应该很好地与 matplotlib 集成?当我用相同的熊猫系列制作条形图/折线图时,它工作得很好,似乎是直方图特有的问题......
  • 作为一般提示,我知道有各种各样的教程/示例使用plt.plot 处理所有内容(plt.xlabel()、plt.imshow()...),但用于实际的程序化绘图,远离 pyplot 并直接使用对象方法(ax.plot,ax.set_xlabel())将使您的生活轻松十倍。

标签: python pandas matplotlib histogram


【解决方案1】:

这是一个有趣的效果,似乎出现在 pandas 0.16.0 中。升级到 0.16.1 可以修复它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2021-03-31
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多