【问题标题】:Matplotlib style not working in pandas bar plotMatplotlib 样式在熊猫条形图中不起作用
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    正如 ali_m 在 cmets 中指出的那样,当使用 matplotlib 1.5.0 版时,它是最新版 pandas (0.17.1) 中的bug

    修复将在下一个 pandas 版本中进行。

    直到新版本出来,我跑了

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

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

    【讨论】:

      猜你喜欢
      • 2017-12-30
      • 2019-04-13
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2022-01-20
      相关资源
      最近更新 更多