python颜色列表代码seaborn_python - 使用Seaborn根据列名称分配颜色 - 堆栈内存溢出...

我正在尝试创建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的小提琴图没有相同的颜色。 我希望每个图表的各列颜色保持一致。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值