matplotlib - Why does the Seaborn color palette not work for Pandas bar plots? -
an online jupyter notebook demonstrating code , showing color differences at: https://anaconda.org/walter/pandas_seaborn_color/notebook
the colors wrong when make bar plots using pandas dataframe method. seaborn improves color palette of matplotlib. plots matplotlib automatically use new seaborn palette. however, bar plots pandas dataframes revert non-seaborn colors. behavior not consistent, because line plots pandas dataframes do use seaborn colors. makes plots appear in different styles, if use pandas plots.
how can plot using pandas methods while getting consistent seaborn color palette?
i'm running in python 2.7.11 using conda environment necessary packages code (pandas, matplotlib , seaborn).
import pandas pd import matplotlib.pyplot plt import seaborn 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 default matplotlib color palette df.plot(kind='bar') plt.show() # pandas line plots correctly use seaborn color palette df.plot() plt.show()
credit @mwaskom pointing sns.color_palette()
. looking somehow missed hence original mess prop_cycle
.
as workaround can set color hand. note how color
keyword argument behaves differently if plotting 1 or several columns.
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'] x in prop_cycle])
Comments
Post a Comment