问题描述
在Pandas中使用数据框df,我试图在同一页面上绘制直方图,并通过3个不同的变量进行过滤;预期的结果是三种类型中每种类型的值的直方图.以下代码对我来说很适合PLOT,但是当我改用HIST时,它们全部相互叠加.有什么建议?
with a dataframe df in Pandas, I am trying to plot histograms on the same page filtering by 3 different variables; the intended outcome is histograms of the values for each of the three types. The following code works for me as PLOTs, but when I change to HISTs, they all stack on top of each other. Any suggestions?
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')
这是熊猫中的错误,还是情节和历史不能互换?
Is this a bug in Pandas or are plot and hist not interchangable?
预先感谢
推荐答案
图和hist不能互换:hist是它自己的 DataFrame 和 Series 方法.您可以通过传入ax关键字来强制绘图独立.所以
Plot and hist aren't interchangabe: hist is its own DataFrame and Series methods. You might be able to force the plots to be independent by passing in an ax keyword. So
fig = plt.figure()
ax1 = fig.add_subplot(3,1,1)
df[df.Type == 'Type1'].values.hist(ax=ax1)
....
这篇关于 pandas 多个地块不能作为历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!