2017-04-16 11 views
2

Hereはmatplotlibを使って作成したサブプロットです。あらかじめ定義された範囲に基づいて色をコード化することは可能ですか?追加のパラメータ電圧を関数drawLoadDurationに渡し、その色を設定する尺度(if-else構造を使用して?)を定義します。電圧が高いほど濃くなります。また、何らかの理由でカラーバーのy目盛りのラベルが表示されません。 いずれの鉛も大歓迎です...ありがとう!その後、カラーマップによって理解される0と1の間の範囲に電圧値をマッピングするようmatplotlibでスカラを描画可能なカラーコード

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() 

答えて

0

あなたは、matplotlib.colors.Normalize(vmin=0, vmax=1000)を正規インスタンスを作成することができます。プロット関数の内部では、この正規化を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() 

enter image description here

関連する問題