简体   繁体   English

Seaborn / Python / Matplotlib的循环自定义调色板

[英]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. 我正在寻找是否有一种方法可以设置调色板,以确保每次绘制图形时,条形的颜色都将与x轴上的值一致。

I am using Seaborn 我正在使用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 我将使用各种不同的图表,不同的条件来使用此图表-有时会包括所有4天,有时不会

If you look at the example below, the colors work fine 如果您看下面的示例,则颜色可以正常工作

Example 1: 范例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: 但是示例2基于周日的筛选:

%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. 在这里,我将使用zip但是for循环或列表/ dict理解也可以使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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