[英]How to set color for each category in plotly bar chart python
So I have done the bar chart with a pandas dataframe. But how can set color for each bar category.所以我用 pandas dataframe 做了条形图。但是如何为每个条形类别设置颜色。
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()
You're looking for explicit color mapping in plotly with plotly.express.bar
and for that you can pass color_discrete_map
as a keywork argument ( dict
) that will hold the color of each value individually.您正在 plotly 和plotly.express.bar
中寻找显式颜色映射,为此您可以将color_discrete_map
作为键参数 ( dict
) 传递,它将单独保存每个值的颜色。
fig = px.bar(
x=labels,
y=value,
height=400,
width=800,
title = "Asset Area Vs No of OBS",
color=labels,
color_discrete_map={
'BAGHJAN': 'green',
'JORAJAN': 'blue'} #<-- add here the others colors
)
NB: An arbitrary color will be assinged to every non mapped value注意:任意颜色将分配给每个非映射值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.