如何更改stackplot,matplotlib的调色板?

evrscar2  于 2023-10-24  发布在  其他
关注(0)|答案(4)|浏览(166)

我希望改变stackplot的调色板,使大面积有一个浅色,小面积有一个明亮的颜色。

  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns
  5. from scipy.stats import norm
  6. import matplotlib as mpl
  7. import matplotlib.font_manager as font_manager
  8. file = r'E:\FD\Barren_Mudflat\ChinaCoastal\Provinces\0ProvinceStat.csv'
  9. #set font property of legend
  10. font1 = {'family' : 'Times New Roman',
  11. 'weight' : 'normal',
  12. 'size' : 16
  13. }
  14. #read csv
  15. dat1 = pd.read_csv(file)
  16. dat2 = dat1.iloc[:,0:12]
  17. Year = dat2.iloc[:,0]
  18. Mud = dat2.iloc[:,1:12]
  19. Mud = Mud/1000.0
  20. #read columns of dataframe
  21. vol = Mud.columns
  22. #transpose mud
  23. mud2 = Mud.T
  24. %matplotlib qt5
  25. #set size of figure
  26. fig, ax = plt.subplots()
  27. fig.set_size_inches(15, 7.5)
  28. #read values of dataframe
  29. value = mud2.values
  30. #plot stack area
  31. sp = ax.stackplot(Year, value)
  32. #set legend
  33. proxy = [mpl.patches.Rectangle((0,0), 0,0, facecolor=pol.get_facecolor()[0])
  34. for pol in sp]
  35. ax.legend(proxy, vol,prop = font1, loc='upper left', bbox_to_anchor=
  36. (0.01,1), ncol = 6)
  37. plt.xlim(1986,2016)
  38. plt.xticks([1986,1991,1996,2001,2006,2011,2016],fontproperties='Times New
  39. Roman', size = '16')
  40. plt.xlabel('Year',fontproperties='Times New Roman', size = '18')
  41. plt.ylim(0,1400)
  42. plt.yticks(np.arange(0,1500,200),fontproperties='Times New Roman', size =
  43. '16')
  44. plt.ylabel('Mudflat area (thousand ha)',fontproperties='Times New Roman',
  45. size = '18')
  46. #save fig: run this code before show()
  47. plt.savefig(r"E:\FD\Barren_Mudflat\ChinaCoastal\Provinces\stackplot.jpg",
  48. dpi = 600)
  49. plt.show()

这是代码的结果,我希望把红色改成浅色,但是不知道怎么改默认颜色

zf9nrax1

zf9nrax11#

对于像我这样的人来说,发现这条线索比创作晚一点。
可以通过HEX颜色代码设置自定义颜色。
例如

  1. color_map = ["#9b59b6", "#e74c3c", "#34495e", "#2ecc71"]

然后绘图:

  1. ax.stackplot(x, y, colors = color_map)

作为最后一个说明,它也可以转换RGB颜色到十六进制颜色(我不得不在我的情况下这样做)。如下所示:

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

zzwlnbp82#

  1. col = sns.color_palette("hls", 11)
  2. sp = ax.stackplot(Year, value, colors = col)
zour9fqk

zour9fqk3#

如果想要一个更有层次的外观,也可以从颜色渐变设置比例

  1. # user to specify
  2. source = stack_lst # list of units to be stacked
  3. pct_max = 95 # for example, max percentile of color ramp
  4. pct_min = 20 # for example, min percentile of color ramp
  5. ramp = plt.cm.viridis # for example
  6. # number of items in data source
  7. n = len(source)
  8. # list of values between 0.00 and 1.00; length equals length of data source
  9. n_prop = list(i / 100.0 for i in (np.arange(pct_min, pct_max, (pct_max-pct_min)/n)))
  10. # create list of colors
  11. clr_lst = []
  12. for i in n_prop:
  13. clr = ramp(i)
  14. clr_lst.append(clr)

然后,

  1. ax.stackplot(x, y, colors = clr_lst)

展开查看全部
lvjbypge

lvjbypge4#

  1. col = matplotlib.cm.Blues(np.linspace(0.5, 1, len(df.columns)))
  2. plt.stackplot(df.index, *df.T.values, labels = df.columns, colors = col)

相关问题