使用Seaborn根据列名称分配颜色

时间:2019-06-26 14:59:51

标签: python colors seaborn palette df

我正在尝试创建4种类型的图表,以汇总我的数据。我已经包含了创建两种图表类型的代码。我正在遍历蜡笔颜色的调色板,以便为每个图表获得唯一的颜色。但是,我希望数据集中每一列的颜色保持一致。

我创建了几行虚假数据。我的数据如下所示:

         Time  type1  type2  type3
0  2015-01-01    100    200    300
1  2015-02-01    150    250    350
2  2015-03-01    300    300    300
3  2015-04-01    350    350    350

代码:

#Setting up data 
import warnings
import itertools
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
import sys
import seaborn as sns 

data = [{'Time': '201501','type1': 100, 'type2': 200, 'type3':300}, 
        {'Time': '201502' ,'type1':150, 'type2': 250, 'type3': 350}, 
        {'Time': '201503' ,'type1':300, 'type2': 300, 'type3': 300}, 
        {'Time': '201504' ,'type1':350, 'type2': 350, 'type3': 350}] 

data = pd.DataFrame(data) 

#Data prep

#setting index
data['Time']=pd.to_datetime(data['Time'], format='%Y%m')
data.set_index(['Time'], inplace=True)
#setting type for line graph
data=data.astype(float)

data

palette = itertools.cycle(sns.color_palette(palette=sns.colors.crayons))



############################# LINE PLOT ##################################################   

#this loops over each column in my data set and produces a graph

for i in data:  # Loop over all columns except 'Location'
    sns.set() #defaults the background
    fig, ax = plt.subplots()
    sns.set(style="ticks") 
    sns.lineplot(x=data.index,y=i,data=data,color=next(palette))  # column is chosen here
    sns.despine(offset=10, trim=True) 
    fig.set_size_inches(18,12)
    ax.set_title('{} History'.format(i), fontweight='bold')

    plt.savefig('{}.pdf'.format(i), bbox_inches='tight')  #sets file name based on column name


############################# VIOLIN PLOT ###############################################         
for i in data:  # Loop over all columns 
    sns.set() #defaults the background
    fig, ax = plt.subplots()
    sns.set(style="ticks") #darkens grid lines
    sns.violinplot(y=i, data=data,color=next(palette))  #sets which column to use
    sns.despine(offset=10, trim=True) 
    fig.set_size_inches(18,12)
    ax.set_title('{} Violin Plot'.format(i), fontweight='bold') #sets chart title based on column
    plt.savefig('{}_violin.pdf'.format(i), bbox_inches='tight')  #sets file name based on column name

enter image description here enter image description here

运行代码时,每个图表都有唯一的颜色,但是折线图和类型1的小提琴图没有相同的颜色。我希望每个图表的各列颜色保持一致。

2 个答案:

答案 0 :(得分:1)

正在从infinitely cycling调色板中选择颜色:

palette = itertools.cycle(sns.color_palette(palette=sns.colors.crayons))
sns.lineplot(..., color=next(palette)) 
...
sns.violinplot(..., color=next(palette))  

每次调用next(palette)时,将返回循环中的下一个颜色。 因此,线图和小提琴图之间没有颜色匹配(除非通过 奇迹般的巧合或设计使len(data.columns)恰好是 {{1}的倍数)。

一种使线条图和小提琴颜色协调的方法是重置调色板 在每个循环之前:

len(sns.colors.crayons)

以上,palette = sns.color_palette(palette=sns.crayon_palette(sns.colors.crayons)) new_palette = itertools.cycle(palette) for i in data: # Loop over all columns except 'Location' ... sns.lineplot(x=data.index, y=i, data=data, color=next(new_palette)) ... new_palette = itertools.cycle(palette) for i in data: # Loop over all columns ... sns.violinplot(y=i, data=data, color=next(new_palette)) 只是一个列表。 palette是无限循环的 迭代器。通过在每个new_palette之前创建一个new_palettefor-loop将在两个next(new_palette)之间以相同的顺序返回相同的颜色。

或者,获得所需结果的一种更简单的方法是将两个for循环组合为一个,以便您可以在同一迭代中调用for-looplineplot并将相同的颜色传递给这两个函数都调用。 将代码分解为功能可以帮助阐明代码的意图并保持代码整洁。

vionlinplot

答案 1 :(得分:1)

由于您在每次通话中继续使用color=next(palette),因此出现了问题。您正在寻找的是列名和颜色之间的恒定映射。您的代码中所需的最小更改是:

...
palette = itertools.cycle(sns.crayon_palette(sns.colors.crayons))  # I get an exception for your version, but I guess this is equivalent to what you meant

palette = dict(zip(data.columns, palette)) # this creates a mapping between columns and colors

...
for i in data: 
    ...
    sns.lineplot(x=data.index, y=i, data=data, color=palette[i])
    ...

这样,每次绘制线条时,所使用的颜色对应于特定的列,而不是从迭代器的next函数中连续绘制。