繁体   English   中英

Matplotlib.axes.Axes.bar 条件格式

[英]Matplotlib.axes.Axes.bar conditional formatting

我在 matplotlib 中有一个垂直条形图,并且希望条形的颜色根据数组中的值而变化。 我知道在 plot.scatter() 中可以使用 cmap='' 来完成。 但我似乎找不到与 plot.bar() 相同的功能。 有什么建议吗? 这里 是有问题的数字。

fig, season = plt.subplots()

# show the phenology season

season.set_ylabel('GPP 20th percent yearly max')
season.tick_params('y', colors = 'blue', labelsize =24)

season.bar(x = pheno['SRO_SoS'], height= pheno['SRO_20th'], width = 
           pheno['SRO_DateDelta'], zorder=1, color = 'wheat', align = 
'edge')

season.set_ylim(0,5)
temp = season.twinx()

temp.plot(df_w.index, df_w['TA_F'],color = 'red', label = 'Tempurature', 
linewidth = 2)

# set x-label 
temp.set_xlabel('Date')
temp.tick_params('x', labelsize =24)

 # set primary y label
temp.set_ylabel('Tempurature (C)')
temp.tick_params('y', colors = 'red', labelsize =24)

# set x-axis limits as the min and max of the series
temp.set_xlim(date2num([df_w.index.min(), df_w.index.max()]))
temp.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
temp.xaxis.set_major_locator(mdates.YearLocator(1, month=1, day=1))

temp.set_ylim(2,30)

temp.grid(True)

plt.show()

您可以为您的season.bar()中的颜色参数提供一个数组。 那应该行得通。

如果你想给出离散的颜色,这里有一个例子。

fig, season = plt.subplots()
clist = ['red', 'blue', 'green']
season.bar(x = range(1, 11), height= range(10, 30, 2), width = 0.8, zorder=1, color = clist, align = 'edge')

离散颜色 - 输出图

在此处输入图像描述

对于使用 cmap 连续颜色,这里是另一个示例。

fig, season = plt.subplots()
my_cmap = plt.get_cmap("viridis")
colorrange = [0,0.25,0.5,0.75,1.0] #Range should be in range of 0 to 1
season.bar(x = range(1, 11), height= range(10, 30, 2), width = 0.8, zorder=1, color=my_cmap(colorrange), align = 'edge')

连续颜色 - 输出图

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM