问题描述
是我使用matplotlib创建的子图.是否可以根据预定义的范围对颜色进行编码?我想将附加参数 电压 传递给函数 drawLoadDuration 并定义设置颜色的比例(使用 if-else 构造?).电压越高,阴影越深.另外,由于某些原因,色标的y刻度标签未显示.任何线索都是最受欢迎的...谢谢!
is a subplot I created using matplotlib. Is it possible to code the colors on the basis of a pre-defined range? I want to pass an additional parameter, voltage to the function drawLoadDuration and define a scale (using if-else construct?) that sets the color. Higher the voltage, darker the shade. Also, for some reason, the y-tick labels for the colorbar are not showing.Any lead is most welcome...Thanks!
import matplotlib.cm
from pylab import *
import numpy as np
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=False)
#other subplots
ax3.set_title('Load Profile')
ax3.patch.set_facecolor('silver')
ax3.grid(True)
cmap= plt.cm.bone_r
barHeight = 3
ticklist = []
def drawLoadDuration(period, starty, opacity):
ax3.broken_barh((period), (starty, barHeight), alpha=opacity, facecolors=cmap(opacity), lw=0.5, zorder=2)
ticklist.append(starty+barHeight/2.0)
return 0
drawLoadDuration([(0, 5), (13, 4), (19, 3), (23, 1)], 3, 0.5) #Fan
drawLoadDuration([(19, 1)], 9, 0.65) #Tube Light
drawLoadDuration([(19, 5)], 15, 0.35) #Bulb
drawLoadDuration([(7, 2), (16, 1)], 21, 0.28) #Charger
drawLoadDuration([(15, 0.5), (20, 1)], 27, 0.7) #Television
drawLoadDuration([(9, 1), (17, 1)], 33, 1) #Pump
drawLoadDuration([(2,4)], 39, 0.8) #Scooter
ax3.set_ylim(0, 45)
ax3.set_xlim(0, 24)
ax3.set_xlabel('Time (Hours)')
ax3.set_yticks(ticklist)
xticklist = np.linspace(0.5, 24, 48)
ax3.set_xticks(xticklist)
ax3.set_xticklabels(["{}{}m".format(int(h%12+12*(h%12==0)),
{0:"p",1:"a"}[(h%24)<12]) if ((h*10)%10)==0 \
else "" for h in xticklist], fontsize='9', rotation=90)
ax3.tick_params('x', colors=cmap(1.0), tick1On=True)
ax3.set_yticklabels(['Fan', 'Tube light', 'Bulb', 'Cellphone Charger', 'Television', 'Pump', 'Scooter'])
######################### Code Block for Colorbar
sm = matplotlib.cm.ScalarMappable(cmap=cmap) # create a scalarmappable from the colormap
sm.set_array([])
cbar = f.colorbar(sm, ticks=[-3, -2, -1, 0, 1, 2, 3], aspect=10, orientation='vertical', ax=ax3) # using scalarmappable to create colorbar
cbar.ax.text(3, 0.65, 'Power', rotation=90)
cbar.ax.set_yticklabels(['>1000', '>800', '>500', '>200', '>100', '<10']) #not working!!!
plt.show()
推荐答案
您可以创建一个规范化实例 matplotlib.colors.Normalize(vmin = 0,vmax = 1000)
以映射电压值在0到1之间的范围内,然后颜色图将理解该值.在绘图函数中,您可以将此归一化用作 facecolors=cmap(norm(voltage))
.
You may create a normalization instance, matplotlib.colors.Normalize(vmin=0, vmax=1000)
as to map the voltage values to the range between 0 and 1, which will then be understood by the colormap. Inside the plotting function you would use this normalization as facecolors=cmap(norm(voltage))
.
import matplotlib.cm
import matplotlib.pyplot as plt
import numpy as np
f, ax3 = plt.subplots()
ax3.set_title('Load Profile')
ax3.patch.set_facecolor('silver')
ax3.grid(True)
cmap= plt.cm.bone_r
# create normalization instance
norm = matplotlib.colors.Normalize(vmin=0, vmax=1000)
# create a scalarmappable from the colormap
sm = matplotlib.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
barHeight = 3
ticklist = []
def drawLoadDuration(period, starty, voltage):
ax3.broken_barh((period), (starty, barHeight), alpha=1,
facecolors=cmap(norm(voltage)), lw=0.5, zorder=2)
ticklist.append(starty+barHeight/2.0)
return 0
drawLoadDuration([(0, 5), (13, 4), (19, 3), (23, 1)], 3, 500) #Fan
drawLoadDuration([(19, 1)], 9, 650) #Tube Light
drawLoadDuration([(19, 5)], 15, 350) #Bulb
drawLoadDuration([(7, 2), (16, 1)], 21, 280) #Charger
drawLoadDuration([(15, 0.5), (20, 1)], 27, 700) #Television
drawLoadDuration([(9, 1), (17, 1)], 33, 1000) #Pump
drawLoadDuration([(2,4)], 39, 800) #Scooter
ax3.set_ylim(0, 45)
ax3.set_xlim(0, 24)
ax3.set_xlabel('Time (Hours)')
ax3.set_yticks(ticklist)
xticklist = np.linspace(0.5, 24, 48)
ax3.set_xticks(xticklist)
ax3.set_xticklabels(["{}{}m".format(int(h%12+12*(h%12==0)),
{0:"p",1:"a"}[(h%24)<12]) if ((h*10)%10)==0 \
else "" for h in xticklist], fontsize='9', rotation=90)
ax3.tick_params('x', colors=cmap(1.0), tick1On=True)
ax3.set_yticklabels(['Fan', 'Tube light', 'Bulb', 'Cellphone Charger', 'Television', 'Pump', 'Scooter'])
######################### Code Block for Colorbar
# using scalarmappable to create colorbar
cbar = f.colorbar(sm, ticks=[10,100,200,500,800,1000], aspect=10, orientation='vertical', ax=ax3, label='Power')
plt.show()
这篇关于在 matplotlib 中使用标量可映射的颜色编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!