简体   繁体   English

plotly 条形图 python 如何设置每个类别的颜色

[英]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()

my output:我的 output: 这是图像

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.

相关问题 如何在 python 中创建堆积条形图,按类别进行颜色编码 - How to create stacked bar chart in python, color coded by category 如何在条形图中同时设置宽度和间隙? (Python,情节) - How to set width and gap simultaneously in a bar chart? (Python, Plotly) 将分组条形图中每个条形图的颜色更改为自定义颜色 - Change color of each bar in a grouped bar chart plotly to custom colors 如何在分组 plotly 条形图中为条形着色 - How to color bars in grouped plotly bar chart 如何控制 plotly 条形图中的彩条重复? - How to control color bar repeating in plotly bar chart? Plotly 条形图 - 根据正值/负值更改颜色 - python - Plotly bar chart - change color based on positive/negative value - python Plotly:如何使用 Python 对 plotly 图形对象条形图进行颜色编码? - Plotly: How to colorcode plotly graph objects bar chart using Python? Plotly:如何使用 go.Figure 和 go.Scatter 为每个 y 误差条设置单独的颜色? - Plotly: How to set individual color for each y error bar using go.Figure and go.Scatter? 我们如何通过 plotly python 中的某些条件为 plotly 条着色? - How can we color the plotly bar by some condition in plotly python? 如何为可绘制图表的0上方和下方的值制作不同的颜色条 - How to make different color bar for value above and below 0 for plotly chart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM