我正在尝试创建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
运行代码时,每个图表都有唯一的颜色,但是折线图和类型1的小提琴图没有相同的颜色。 我希望每个图表的各列颜色保持一致。