Why doesn't the Seaborn color palette work for Pandas line art?

The Jupyter online store showing code and color differences is located at: https://anaconda.org/walter/pandas_seaborn_color/notebook

The colors are wrong when I do line art using the Pandas dataframe method. Seaborn improves the matplotlib color palette. All graphs from matplotlib automatically use the new Seaborn palette. However, line art from Pandas data frames reverts to colors not related to the sea. This behavior is consistent because the graphs from Pandas dataframes do use Seaborn colors. This makes my stories look like different styles, even if I use Pandas for all of my stories.

How can I build using Pandas methods while getting a consistent Seaborn color palette?

I run this in python 2.7.11 using a conda environment with only the necessary packages for this code (pandas, matplotlib and 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() 
+5
source share
2 answers

@Mwaskom is credited for pointing to sns.color_palette() . I was looking for this, but somehow I missed it, so the original mess with prop_cycle .


As a workaround, you can set the color manually. Notice how the color keyword argument behaves differently if you draw one or more 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)) 

Single column chart

 df.plot(kind='bar', color=sns.color_palette()) 

Two column chart

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]) 
+5
source

This was a bug in pandas specifically for bar charts (and boxes) that were fixed in pandas master (see issue and PR message to fix this).
It will be in pandas 0.18.0, which will be released in the coming weeks.

+2
source

Source: https://habr.com/ru/post/1243630/


All Articles