简体   繁体   English

如何为可绘制图表的0上方和下方的值制作不同的颜色条

[英]How to make different color bar for value above and below 0 for plotly chart

I have generated a bar chart through plotly and how can i make the bar above 0 green and the bar below 0 red? 我已经通过绘图生成了条形图,如何使条形图在0上方绿色,而条形图在0以下红色?

import plotly
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True)
trace1 = go.Bar(
    x=df.symbol,
    y=df["percentageChange30dBtc"],

    name='Top10',
    marker = dict(color = 'rgba(63, 195, 128, 1)', 
                              line = dict(color='rgb(0,0,0)',width=1.5)),
   text=df.percentageChange30dBtc,
        textposition='outside'
)
##rgba(252, 214, 112, 1),'rgba(255,174,255,0.5)'
data = [trace1]
plotly.offline.iplot({
    "data": data,
    "layout": go.Layout(barmode='group', yaxis=dict(tickformat=".0%"),title="24H Change Binance"  #tickformat=".0%"
     ,width=800,height=600,)
})

在此处输入图片说明

you can pass a list of colors on your marker, (assuming that df["percentageChange30dBtc"] values are numeric, if is string ending with % do float(x.replace('%',''))>0 instead of x>0 您可以在标记上传递颜色列表(假设df [“ percentageChange30dBtc”]的值是数字,如果以%结尾的字符串是float(x.replace('%',''))>0而不是x>0

import plotly
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True)
trace1 = go.Bar(
    x=df.symbol,
    y=df["percentageChange30dBtc"],

    name='Top10',
    marker = dict(color = ['rgba(63, 195, 128, 1)' if x>0 else 'rgba(219, 10, 91, 1)' for x in df["percentageChange30dBtc"]], 
                 line = dict(color='rgb(0,0,0)',width=1.5)),
   text=df.percentageChange30dBtc,
        textposition='outside'
)
##rgba(252, 214, 112, 1),'rgba(255,174,255,0.5)'
data = [trace1]
plotly.offline.iplot({
    "data": data,
    "layout": go.Layout(barmode='group', yaxis=dict(tickformat=".0%"),title="24H Change Binance"  #tickformat=".0%"
     ,width=800,height=600,)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM