[英]Matplotlib.axes.Axes.bar conditional formatting
I have a vertical bar plot in matplotlib and would like the color of the bars to vary based on the values in an array.我在 matplotlib 中有一个垂直条形图,并且希望条形的颜色根据数组中的值而变化。 I know that in plot.scatter() this can be done with cmap=''.
我知道在 plot.scatter() 中可以使用 cmap='' 来完成。 But i can't seem to find the same functionality with plot.bar().
但我似乎找不到与 plot.bar() 相同的功能。 Any advice?
有什么建议吗?
is the figure in question.
是有问题的数字。
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()
You can give an array to the color argument in your season.bar()
.您可以为您的
season.bar()
中的颜色参数提供一个数组。 That should work.那应该行得通。
If you want to give discrete colors, here is an example.如果你想给出离散的颜色,这里有一个例子。
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')
Discrete colors - Output plot离散颜色 - 输出图
For using cmap continuous colors, here is another example.对于使用 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')
Continuous colors - Output plot连续颜色 - 输出图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.