Home > database >  How to set color for each category in plotly bar chart python
How to set color for each category in plotly bar chart python

Time:01-10

So I have done the bar chart with a pandas dataframe. But how can set color for each bar category.

This is my code .

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

data = {'Index': ['NAGAJAN', 'JORAJAN','KATHALGURI','HEBEDA','MAKUM','BAREKURI','BAGHJAN','Duliajan Area','LANGKASHI','HAPJAN'],'Category': [0,5,0,0,2,0, 2,0,0, 2]} 
df = pd.DataFrame(data)
labels =df['Index']
value = df['Category']
fig = px.bar(x=labels,y=value, height=400, width=800,title = "Asset Area Vs No of OBS")
fig.update_xaxes(tickangle = 90,)
fig.update_layout(plot_bgcolor="white")
fig.update_traces(width=0.4)
fig.show()

my output : This is the image

CodePudding user response:

You're looking for enter image description here

NB : An arbitrary color will be assinged to every non mapped value

  • Related