【发布时间】:2016-06-04 19:54:32
【问题描述】:
演示代码并显示颜色差异的在线 Jupyter 笔记本位于: https://anaconda.org/walter/pandas_seaborn_color/notebook
当我使用 Pandas 数据框方法制作条形图时,颜色错误。 Seaborn 改进了 matplotlib 的调色板。 matplotlib 中的所有绘图都会自动使用新的 Seaborn 调色板。但是,来自 Pandas 数据帧的条形图会恢复为非 Seaborn 颜色。这种行为并不一致,因为 Pandas 数据框的线图确实使用 Seaborn 颜色。这使我的情节看起来具有不同的风格,即使我的所有情节都使用 Pandas。
如何使用 Pandas 方法进行绘图,同时获得一致的 Seaborn 调色板?
我在 python 2.7.11 中使用 conda 环境运行此代码,其中仅包含此代码所需的包(pandas、matplotlib 和 seaborn)。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({'y':[5,7,3,8]})
# matplotlib figure correctly uses Seaborn color palette
plt.figure()
plt.bar(df.index, df['y'])
plt.show()
# pandas bar plot reverts to default matplotlib color palette
df.plot(kind='bar')
plt.show()
# pandas line plots correctly use seaborn color palette
df.plot()
plt.show()
【问题讨论】:
-
1. 您的具体问题是什么?您想知道解决方案(和可能的解决方法)还是想知道这种行为的原因? 2. 此信息可能与您的问题有关:
pyplot.scatter也不使用 seaborn 颜色(默认情况下?)。 -
感谢 kazemakase。我已经编辑了这个问题,以澄清我想知道如何使用 Pandas 方法进行绘图,同时获得一致的 Seaborn 调色板。 (当然,知道原因可能对上述有所帮助。)
标签: pandas matplotlib seaborn