Pandas 中奇怪的条形图对齐方式 [英] Strange bar plot alignment in Pandas

查看:47
本文介绍了Pandas 中奇怪的条形图对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下命令在 Pandas 中生成条形图(其中 x 是现有的 DataFrame):

I'm generating a bar plot in pandas using the following commands (where x is an existing DataFrame):

df = x.groupby(['pAlt']).describe()['win_stay'].unstack()
df['se'] = df['std']/np.sqrt(df['count']) # calculate standard error
df['mean'].plot(kind='bar',yerr=df.se,alpha=0.5,ax=ax,legend=False)

除了条形的位置外,情节总体上看起来是正确的:

The plot generally looks right, except for the position of the bars:

出于某种原因,它们靠在情节的右边缘,而不是居中.这似乎是 Pandas 0.14 中引入的一个新问题:如果我降级到 0.13.1 并运行完全相同的代码,则情节如下所示:

For some reason they're butted up against the right edge of the plot, rather than being centered. This appears to be a new issue introduced in Pandas 0.14: If I downgrade to 0.13.1 and run the exact same code, the plot looks like this:

除了坚持使用降级版本的熊猫之外,还有其他简单的解决方法吗?

Any simple workarounds other than sticking with the downgraded version of pandas?

推荐答案

这可能与 matplotlib 中的这个错误,影响了某些版本1.4.0.(我在 1.3.1 版中看到它).

This might be related to this bug in matplotlib, affecting some versions < 1.4.0. (I'm seeing it in version 1.3.1).

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl

print(pd.__version__)
# 0.14.0
print(mpl.__version__)    
# 1.3.1

fig, ax = plt.subplots()
df = pd.DataFrame({'mean': [0.25, 0.2, 0.25]}, index=[0.5, 0.8, 0.85])
df['mean'].plot(kind='bar', alpha=0.5, legend=False, ax=ax)

ax.set_xlim(-1, len(df['mean']))
plt.show()

这篇关于Pandas 中奇怪的条形图对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆