Matplotlib风格不适用于熊猫条形图

时间:2015-12-30 15:07:28

标签: python pandas matplotlib plot

我用来根据pandas Visualization tutorial生成条形图的代码。

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('ggplot')

np.random.seed(123456)
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()

plt.figure()
df.ix[5].plot(kind='bar'); plt.axhline(0, color='k')
plt.show()

我明白了:

bar plot with matplotlib default colours when ggplot colours expected

我期望获得教程(朱红色)中的条形颜色,而条形图是默认的matplotlib蓝色。

我怀疑这个问题出在熊猫身上。不使用pandas时颜色正确。

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('ggplot')

fig, ax = plt.subplots()
x = np.arange(5)
y1, y2 = np.random.randint(1, 25, size=(2, 5))
width = 0.25
ax.bar(x, y1, width)
ax.bar(x + width, y2, width)
ax.set_xticks(x + width)
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])

plt.show()

我使用conda创建了我的环境。

In [22]: pd.__version__
Out[22]: u'0.17.1'

我怎样才能让大熊猫画出正确的颜色?

2 个答案:

答案 0 :(得分:0)

正如注释中指出的ali_m,当使用matplotlib 1.5.0版时,它是最新版本的pandas(0.17.1)中的bug

修复将在下一个pandas发布中。

在新版本发布之前,我跑了

pip install git+git://github.com/pydata/pandas.git@master

在我的conda环境中获取最新的修复程序。

答案 1 :(得分:0)

此错误现在似乎已修复。当我在原始问题中运行代码时,我得到了朱红色条。