为什么 Seaborn 调色板不适用于 Pandas 条形图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35569738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does the Seaborn color palette not work for Pandas bar plots?
提问by Walter
An online Jupyter notebook demonstrating the code and showing the color differences is at: https://anaconda.org/walter/pandas_seaborn_color/notebook
演示代码并显示颜色差异的在线 Jupyter 笔记本位于:https://anaconda.org/walter/pandas_seaborn_color/notebook
The colors are wrong when I make bar plots using Pandas dataframe method. Seaborn improves the color palette of matplotlib. All plots from matplotlib automatically use the new Seaborn palette. However, bar plots from Pandas dataframes revert to the non-Seaborn colors. This behavior is not consistent, because line plots from Pandas dataframes douse Seaborn colors. This makes my plots appear to be in different styles, even if I use Pandas for all my plots.
当我使用 Pandas 数据框方法制作条形图时,颜色是错误的。Seaborn 改进了 matplotlib 的调色板。matplotlib 中的所有绘图都会自动使用新的 Seaborn 调色板。但是,来自 Pandas 数据框的条形图恢复为非 Seaborn 颜色。这种行为不一致,因为来自 Pandas 数据框的线图确实使用了 Seaborn 颜色。这使我的情节看起来具有不同的风格,即使我在所有情节中都使用 Pandas。
How can I plot using Pandas methods while getting a consistent Seaborn color palette?
如何在获得一致的 Seaborn 调色板的同时使用 Pandas 方法进行绘图?
I'm running this in python 2.7.11 using a conda environment with just the necessary packages for this code (pandas, matplotlib and 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()
回答by Stop harming Monica
Credit to @mwaskom for pointing to sns.color_palette()
. I was looking for that but somehow I missed it hence the original mess with prop_cycle
.
感谢@mwaskom 指向sns.color_palette()
. 我一直在寻找它,但不知何故我错过了它,因此原来的prop_cycle
.
As a workaround you can set the color by hand. Note how the color
keyword argument behaves differently if you are plotting one or several columns.
作为解决方法,您可以手动设置颜色。请注意,color
如果您绘制一列或几列,关键字参数的行为有何不同。
df = pd.DataFrame({'x': [3, 6, 1, 2], 'y':[5, 7, 3, 8]})
df['y'].plot(kind='bar', color=sns.color_palette(n_colors=1))
df.plot(kind='bar', color=sns.color_palette())
My original answer:
我原来的回答:
prop_cycle = plt.rcParams['axes.prop_cycle']
df['y'].plot(kind='bar', color=next(iter(prop_cycle))['color'])
df.plot(kind='bar', color=[x['color'] for x in prop_cycle])
回答by joris
This was a bug in pandas specifically for bar plots (and boxplots as well I think), which is fixed in pandas master (see the reported issueand the PRto fix it).
This will be in pandas 0.18.0 which will be released in the coming weeks.
这是 Pandas 中专门针对条形图(我认为还有箱线图)的错误,已在 Pandas master 中修复(请参阅报告的问题和PR来修复它)。
这将在 Pandas 0.18.0 中,它将在未来几周内发布。