在matplotlib中使用标量可映射的颜色编码

在matplotlib中使用标量可映射的颜色编码,matplotlib,colormap,Matplotlib,Colormap,是我使用matplotlib创建的子地块。是否可以根据预定义的范围对颜色进行编码?我想向函数drawLoadDuration传递一个附加参数voltage,并定义一个设置颜色的比例(使用if-else构造?)。电压越高,阴影越暗。此外,由于某些原因,颜色栏的y-tick标签未显示。 欢迎任何线索…谢谢 import matplotlib.cm from pylab import * import numpy as np f, (ax1, ax2, ax3) = plt.subplots(3,

是我使用matplotlib创建的子地块。是否可以根据预定义的范围对颜色进行编码?我想向函数drawLoadDuration传递一个附加参数voltage,并定义一个设置颜色的比例(使用if-else构造?)。电压越高,阴影越暗。此外,由于某些原因,颜色栏的y-tick标签未显示。 欢迎任何线索…谢谢

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()
import matplotlib.cm
从派拉布进口*
将numpy作为np导入
f、 (ax1、ax2、ax3)=plt.子批(3,sharex=True,sharey=False)
#其他子地块
ax3.设置标题(“加载配置文件”)
ax3.patch.set_facecolor(“银色”)
ax3.grid(真)
cmap=plt.cm.bone\u r
杆高=3
清单=[]
def drawLoadDuration(期间、起始时间、不透明度):
ax3.断条条((周期),(起始,条高),alpha=不透明度,facecolors=cmap(不透明度),lw=0.5,zorder=2)
附加(starty+barHeight/2.0)
返回0
拉负荷持续时间([(0,5),(13,4),(19,3),(23,1)],3,0.5)#
拉负荷持续时间([(19,1)],9,0.65)#管灯
拉负荷持续时间([(19,5)],15,0.35)#灯泡
牵引负荷持续时间([(7,2),(16,1)],21,0.28)#
拉负荷持续时间([(15,0.5),(20,1)],27,0.7)#
抽油持续时间([(9,1),(17,1)],33,1)#泵
牵引负荷持续时间([(2,4)],39,0.8)#小型摩托车
ax3.set_ylim(0,45)
ax3.set_xlim(0,24)
ax3.设置标签(“时间(小时)”
ax3.设置检查(勾选列表)
xticklist=np.linspace(0.5,24,48)
ax3.set_xticks(xticklist)
ax3.setxticklabels([“{}{}m”.format(int(h%12+12*(h%12==0)),

{0:“p”,1:“a”}[(h%24)您可以创建一个规范化实例,
matplotlib.colors.Normalize(vmin=0,vmax=1000)
来将电压值映射到0到1之间的范围,这将被colormap理解。在绘图函数中,您可以将此规范化用作
facecolors=cmap(norm(voltage))

import matplotlib.cm
将matplotlib.pyplot作为plt导入
将numpy作为np导入
f、 ax3=plt.子批次()
ax3.设置标题(“加载配置文件”)
ax3.patch.set_facecolor(“银色”)
ax3.grid(真)
cmap=plt.cm.bone\u r
#创建规范化实例
norm=matplotlib.colors.Normalize(vmin=0,vmax=1000)
#从colormap创建ScalarMapable
sm=matplotlib.cm.ScalarMappable(cmap=cmap,norm=norm)
sm.set_数组([]))
杆高=3
清单=[]
def牵引负载持续时间(周期、起始、电压):
ax3.断条((周期),(起点,条高),α=1,
面部颜色=cmap(标准(电压)),lw=0.5,zorder=2)
附加(starty+barHeight/2.0)
返回0
拉负荷持续时间([(0,5),(13,4),(19,3),(23,1)],3500)#
拉负荷持续时间([(19,1)],9650)#管灯
拉负荷持续时间([(19,5)],15,350)#灯泡
拉负荷持续时间([(7,2),(16,1)],21280)#
拉负荷持续时间([(15,0.5),(20,1)],27,700)#
抽油量持续时间([(9,1),(17,1)],33,1000)#泵
牵引负荷持续时间([(2,4)],39800)#滑板车
ax3.set_ylim(0,45)
ax3.set_xlim(0,24)
ax3.设置标签(“时间(小时)”
ax3.设置检查(勾选列表)
xticklist=np.linspace(0.5,24,48)
ax3.set_xticks(xticklist)
ax3.setxticklabels([“{}{}m”.format(int(h%12+12*(h%12==0)),
{0:“p”,1:“a”}[(h%24)
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()