根据if-else语句在条形图中设置颜色

时间:2017-12-12 18:31:54

标签: python plotly

我正在使用python中的一些json数据。我想绘制一个bargragh,以便根据一系列条件显示颜色。我遇到了一个只有满足其他条件的问题。问[' AQI']有不同位置的AQI值,我想根据AQI颜色代码在我的图表中使用颜色

以下是我工作的代码:

import plotly.plotly as py
import plotly.graph_objs as go

def colors(y):
    color1='#ffff00'#setting default value
    if((Q['AQI']>=51).all() & (Q['AQI']<=100).all()):
        color1='rgb(0,222,0)'
    elif((Q['AQI']>=101).all() & (Q['AQI']<=150).all()):
        color1='rgb(0,0,222)'
    elif((Q['AQI']>=151).all() & (Q['AQI']<=200).all()):
        color1='rgb(0,222,0)'    
    elif((Q['AQI']>=201).all() & (Q['AQI']<=300).all()):
        color1='rgb(222,0,0)'   
    elif((Q['AQI']>=301).all() & (Q['AQI']<=600).all()):
        color1='rgb(0,0,222)'
    else: color1='rgb(222,0,0)'

    return color1

trace0 = go.Bar(x=Q['location'],
                y=Q['AQI'],
                marker=dict(color=colors(y)))
data = [trace0]
fig = go.Figure(data=data)
py.iplot(fig, filename='color-bar')

0 个答案:

没有答案