2017-03-25 3 views
0

저는 현재 플롯으로 파이썬에서 간단한 그래프를 생성하고 있습니다. 그래프는 매 시간 큰 지역의 예상 에너지 소비를 나타냅니다. 내가 원하는 것은 해당 영역의 예측 된 에너지 소비량이 빨간색으로 높고 녹색으로 낮 으면 각 막대의 색을 변경하는 것입니다. 높은 값과 낮은 값은 매일 계산되므로 일정하지 않습니다. 음모를 사용하여이 작업을 수행 할 수있는 방법이 있습니까?값에 따라 막대 색상 변경

+1

Plotly 큰 문서를 가지고 https://plot.ly/python/bar-charts/# customizing-individual-bar-colors –

답변

3

음모를 꾸미고 재미있는 일을하는 것처럼 보입니다. 여기

는 일부 데이터를 백업 위조 사용하여 plotly 플롯이다 :

import plotly.plotly as py 
import plotly.graph_objs as go 
import random 
import datetime 

# setup the date series 
# we need day of week (dow) and if it is a weekday (wday) too 
sdate = datetime.datetime.strptime("2016-01-01", '%Y-%m-%d').date() 
edate = datetime.datetime.strptime("2016-02-28", '%Y-%m-%d').date() 
ndays = (edate - sdate).days + 1 
dates = [sdate + datetime.timedelta(days=x) for x in range(ndays)] 
dow = [(x + 5) % 7 for x in range(ndays)] 
wday = [1 if dow[x]<=4 else 0 for x in range(ndays)] 

# now some fake power consumption 
# weekdays will have 150 power consumption on average 
# weekend will have 100 power consumption on average 
# and we add about 20 in random noise to both 
pwval = [90 + wday[x] * 50 + random.randrange(0, 20) for x in range(ndays)] 
# limits - higher limits during the week (150) compared to the weekend (100) 
pwlim = [150 if dow[x] <= 4 else 100 for x in range(ndays)] 

# now the colors 
clrred = 'rgb(222,0,0)' 
clrgrn = 'rgb(0,222,0)' 
clrs = [clrred if pwval[x] >= pwlim[x] else clrgrn for x in range(ndays)] 

# first trace (layer) is our power consumption bar 
trace0 = go.Bar(
    x=dates, 
    y=pwval, 
    name='Power Consumption', 
    marker=dict(color=clrs) 
) 
# second trace is our line showing the power limit 
trace1 = go.Scatter( 
    x=dates, 
    y=pwlim, 
    name='Power Limit', 
    line=dict(
     color=('rgb(0,0,222)'), 
     width=2, 
     dash='dot') 
) 
data = [trace0, trace1] 
layout = go.Layout(title='Power') 
fig = go.Figure(data=data, layout=layout) 
py.iplot(fig, filename='power-limits-1') 

을 그리고이는 모습입니다 :

enter image description here

+0

고마워요! 그것은 나를 위해 잘 일했다! – theodor

+0

그 답을 수락 할 수 있습니까? 초록색 체크를 클릭하기 만하면됩니다. –

+0

물론 가능합니다! 지연 돼서 죄송합니다! :) – theodor

관련 문제