Python 如何更改stackplot、matplotlib的调色板?

Python 如何更改stackplot、matplotlib的调色板?,python,matplotlib,color-palette,Python,Matplotlib,Color Palette,我希望更改stackplot的调色板,以便大区域具有浅色,小区域具有明亮的颜色 import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from scipy.stats import norm import matplotlib as mpl import matplotlib.font_manager as font_manager file = r'E:\FD\

我希望更改stackplot的调色板,以便大区域具有浅色,小区域具有明亮的颜色

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm
import matplotlib as mpl
import matplotlib.font_manager as font_manager

file = r'E:\FD\Barren_Mudflat\ChinaCoastal\Provinces\0ProvinceStat.csv'
#set font property of legend
font1 = {'family' : 'Times New Roman',  
'weight' : 'normal',  
'size'   : 16
} 

#read csv
dat1 = pd.read_csv(file)
dat2 = dat1.iloc[:,0:12]
Year = dat2.iloc[:,0]
Mud = dat2.iloc[:,1:12]
Mud = Mud/1000.0

#read columns of dataframe
vol = Mud.columns

#transpose mud
mud2 = Mud.T


%matplotlib qt5

#set size of figure
fig, ax = plt.subplots()
fig.set_size_inches(15, 7.5)

#read values of dataframe
value = mud2.values
#plot stack area
sp = ax.stackplot(Year, value)
#set legend
proxy = [mpl.patches.Rectangle((0,0), 0,0, facecolor=pol.get_facecolor()[0]) 
for pol in sp]
ax.legend(proxy, vol,prop = font1, loc='upper left', bbox_to_anchor= 
(0.01,1), ncol = 6)

plt.xlim(1986,2016)
plt.xticks([1986,1991,1996,2001,2006,2011,2016],fontproperties='Times New 
Roman', size = '16')
plt.xlabel('Year',fontproperties='Times New Roman', size = '18')
plt.ylim(0,1400)
plt.yticks(np.arange(0,1500,200),fontproperties='Times New Roman', size = 
'16')
plt.ylabel('Mudflat area (thousand ha)',fontproperties='Times New Roman', 
size = '18')

#save fig: run this code before show()
plt.savefig(r"E:\FD\Barren_Mudflat\ChinaCoastal\Provinces\stackplot.jpg", 
dpi = 600)
plt.show()
这是代码的结果。我希望将红色改为浅色,但我不知道如何更改默认的托盘颜色。

对于像我这样的人来说,发现这条线索要比创建时间晚一些

可以通过十六进制颜色代码设置自定义颜色

例如

然后绘制:

ax.stackplot(x, y, colors = color_map)
最后一点,还可以将RGB颜色转换为十六进制颜色(在我的例子中,我必须这样做)。详情如下:

rgb_code = [128, 128, 128]
hex_color = '#%02x%02x%02x' % (rgb_code[0], rgb_code[1], rgb_code[2])

对于像我这样的人来说,发现这条线索要比创作晚一些

可以通过十六进制颜色代码设置自定义颜色

例如

然后绘制:

ax.stackplot(x, y, colors = color_map)
最后一点,还可以将RGB颜色转换为十六进制颜色(在我的例子中,我必须这样做)。详情如下:

rgb_code = [128, 128, 128]
hex_color = '#%02x%02x%02x' % (rgb_code[0], rgb_code[1], rgb_code[2])

如果想要更分级的外观,也可以从颜色渐变按比例设置

# user to specify
source = stack_lst # list of units to be stacked
pct_max = 95 # for example, max percentile of color ramp
pct_min = 20 # for example, min percentile of color ramp
ramp = plt.cm.viridis # for example

# number of items in data source
n = len(source)

# list of values between 0.00 and 1.00; length equals length of data source
n_prop = list(i / 100.0 for i in (np.arange(pct_min, pct_max, (pct_max-pct_min)/n)))

# create list of colors
clr_lst = []
for i in n_prop:
    clr = ramp(i) 
    clr_lst.append(clr)
那么


如果想要更分级的外观,也可以从颜色渐变按比例设置

# user to specify
source = stack_lst # list of units to be stacked
pct_max = 95 # for example, max percentile of color ramp
pct_min = 20 # for example, min percentile of color ramp
ramp = plt.cm.viridis # for example

# number of items in data source
n = len(source)

# list of values between 0.00 and 1.00; length equals length of data source
n_prop = list(i / 100.0 for i in (np.arange(pct_min, pct_max, (pct_max-pct_min)/n)))

# create list of colors
clr_lst = []
for i in n_prop:
    clr = ramp(i) 
    clr_lst.append(clr)
那么