熊猫奇怪的条形图对齐

时间:2014-07-02 20:01:23

标签: python pandas

我正在使用以下命令在pandas中生成条形图(其中x是现有的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)

除了酒吧的位置外,情节通常看起来正确:

enter image description here

出于某种原因,他们与情节的右边缘对接,而不是居中。这似乎是Pandas 0.14中引入的一个新问题:如果我降级到0.13.1并运行完全相同的代码,情节看起来像这样:

enter image description here

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

1 个答案:

答案 0 :(得分:1)

这可能与this bug in matplotlib有关,影响了某些版本< 1.4.0。 (我在版本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()

enter image description here