【问题标题】:pandas multiple plots not working as hists熊猫多个地块不能作为主机工作
【发布时间】:2013-12-03 12:22:36
【问题描述】:

使用 Pandas 中的数据框 df,我试图在同一页面上绘制直方图,通过 3 个不同的变量进行过滤;预期的结果是三种类型中每一种的值的直方图。以下代码适用于我作为 PLOT,但是当我更改为 HIST 时,它们都堆叠在一起。有什么建议?

plt.figure(1)

plt.subplot(311)
df[df.Type == 'Type1'].values.plot()

plt.subplot(312)
df[df.Type == 'Type2'].values.plot()

plt.subplot(313)
df[df.Type == 'Type3'].values.plot()

plt.savefig('output.pdf')

这是 Pandas 中的错误还是 plot 和 hist 不可互换?

提前致谢

【问题讨论】:

  • 你能给我们看看你得到了什么吗?或者提供虚假数据以便我们进行测试?

标签: python matplotlib pandas


【解决方案1】:

Plot 和 hist 不能互换: hist 是它自己的 DataFrameSeries 方法。您可以通过传入 ax 关键字来强制绘图独立。所以

fig = plt.figure()
ax1 = fig.add_subplot(3,1,1)
df[df.Type == 'Type1'].values.hist(ax=ax1)
....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2014-07-25
    • 2018-05-29
    相关资源
    最近更新 更多