简体   繁体   中英

Recurring Custom Color Palette for Seaborn/Python/Matplotlib

I am looking to see if there is a way to set up a color palette to make sure that any time I graph, the colors of the bars will be consistent with the values on the x axis.

I am using Seaborn

I'm sure this is somewhere online, but have been searching for over an hour with no luck

For example, I would want:

"Thursday" to always map to purple, "Friday" to always map to red, "Saturday" to always map to green, "Sunday" to always map to blue,

I will be using this for a variety of different charts, using different criteria - sometimes all 4 days will be included, sometimes they won't be

If you look at the example below, the colors work fine

Example 1:

%matplotlib inline
import pandas as pd
import seaborn as sns
df=pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-   book/master/ch08/tips.csv", sep=',')
df=df[(df['sex']=='Female')&(df['smoker']=='No')&(df['time']=='Dinner')&(df['size']==2)]

看起来如下

But Example 2 , based on the filtering out of Sunday:

%matplotlib inline
import pandas as pd
import seaborn as sns
df=pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-   book/master/ch08/tips.csv", sep=',')
df=df[(df['sex']=='Female')&(df['smoker']=='No')&(df['time']=='Dinner')&(df['size']==2)]
df=df[df['day']!='Sun']

看起来如下

Well, notice how all of the colors shifted

How do I set up a custom Palette that gets mapped to a master list of values (ie Monday = Red, Tuesday=Green, Wednesday=Blue, etc). Dynamically would be great, but static list is ok also

I know I can do something like:

pal = dict(Sat="k", Thur="b", Fri="y", Sun="#9b59b6")

x=sns.barplot(x="day",y="tip",data=df, palette=pal)

Which sets up a palette and then applies it to each bar, but I was more hoping to be able to use an existing palette, something like

sns.color_palette("Set1", n_colors=30, desat=.5)

And then iterate through the palette and assign each color to a value (ie first value is monday, second tuesday, etc)

Would appreciate any help!

Thank you!

Jeff

Just do as you say: "iterate through the palette and assign each color to a value". Here I will use zip but a for loop or a list/dict comprehension would work as well.

df = pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-book/master/ch08/tips.csv", sep=',')
df1 = df[(df['sex']=='Female')&(df['smoker']=='No')&(df['time']=='Dinner')&(df['size']==2)]
df2 = df1[df1['day']!='Sun']

days = df['day'].unique()
pal = dict(zip(days, sns.color_palette("Set1", n_colors=len(days))))

sns.barplot(x="day",y="tip",data=df1, palette=pal)

DF1

sns.barplot(x="day",y="tip",data=df2, palette=pal)

DF2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM