简体   繁体   中英

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?

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

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,)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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