【发布时间】:2016-04-04 13:46:53
【问题描述】:
我用来根据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()
我明白了:
我希望得到教程中的条形颜色(朱红色),而不是条形图是默认的 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'
如何让 pandas 绘制正确的颜色?
【问题讨论】:
-
好吧,当我在我的机器上运行你的代码时,我没有得到
default matplotlib blue颜色。 -
你使用的是什么版本的 matplotlib?听起来像this bug in pandas,它会影响matplotlib 1.5.0。
-
Fwiw,我确实得到了一个蓝色条形图。 Mpl 1.5.0,PD 0.17.1。是的,那个错误报告看起来像你的问题。它确实在其中一个回复中显示了解决方案;希望对你有用。
-
@ali_m Mpl 1.5.0。感谢您提供链接 - 我找到了解决方案。
-
@Dimitri 你能写下你自己问题的答案吗?
标签: python pandas matplotlib plot