[英]How to color bars in grouped plotly bar chart
So, I want to create a bar chart with Plotly.express in Python 3.9.所以,我想在 Python 3.9 中用 Plotly.express 创建一个条形图。 I created the following table with pandas:
我用 pandas 创建了下表:
idx cat1 cat2 cat3 cat4
0 A 0.98 0.93 0.550 1.00
1 B 0.92 0.80 0.865 1.00
2 C 0.91 0.87 0.955 0.96
and I have the following code:我有以下代码:
# df is the pandas table
fig = px.bar(df,
x = 'idx',
y=['cat1', 'cat2', 'cat3', 'cat4']
)
colors = ['#dc1a22', ] * 12
colors[2] = '#000000'
fig.update_traces(marker_color = colors, marker_line_color='rgb(8,48,107)',
marker_line_width=1.5, opacity=0.6)
fig.update_layout(barmode='group')
I want to color all bars in all groups in #dc1a22 except the one at third position (colors[2]).我想为#dc1a22 中所有组中的所有条着色,除了第三个 position (颜色 [2])。
The reality is, that all bars in the first and second group are in #dc1a22 and all bars in the third group are in #000000.现实情况是,第一组和第二组中的所有条都在#dc1a22 中,第三组中的所有条都在#000000 中。
Is there a possibility to color a single bar in a grouped chart?是否有可能在分组图表中为单个条形着色?
Might be important: In the future I am trying to color the bars depending on their values.可能很重要:将来我会尝试根据它们的值对条形进行着色。
(0-0.5 -> red, 0.5-0.9 -> yellow, 0.9-1 -> green) (0-0.5 -> 红色,0.5-0.9 -> 黄色,0.9-1 -> 绿色)
What you can do is add the traces one at a time using a loop, and passing a list of colors and a list of the corresponding column names to each go.bar
object.您可以做的是使用循环一次添加一个跟踪,并将 colors 列表和相应列名列表传递给每个
go.bar
object。 Unfortunately, the default width of the bars will need to be reset, and in grouped mode, the bars are plotted in overlay
mode (meaning if you set the width of each bar without changing the offset, they will overlap).不幸的是,需要重置条的默认宽度,并且在分组模式下,条以
overlay
模式绘制(这意味着如果您在不更改偏移量的情况下设置每个条的宽度,它们将重叠)。
So the offset also needs to be adjusted to make sure the bars are centered nicely around each idx category on the x-axis.因此还需要调整偏移量,以确保条形图很好地围绕 x 轴上的每个 idx 类别居中。 With a bar width of 0.2, setting the offset for each of the four bars to [-0.4, -0.2, 0, 0.2], respectively works.
如果条形宽度为 0.2,则将四个条形中的每一个的偏移量分别设置为 [-0.4, -0.2, 0, 0.2] 即可。 This can probably be generalized for any width.
这可能适用于任何宽度。
import numpy as np
import pandas as pd
import plotly.graph_objs as go
## recreate the data
df = pd.DataFrame({'idx':['A','B','C'], 'cat1':[0.98,0.92,0.91], 'cat2':[0.93,0.80,0.87], 'cat3':[0.55,0.865,0.955], 'cat4':[1,1,0.96]})
fig = go.Figure()
indexes = ['A','B','C']
groups = ['cat1','cat2','cat3','cat4']
colors = ['#dc1a22','#dc1a22','#000000','#dc1a22']
## this can be generalized a bit better
width = 0.2
offset = [-0.4,-0.2,0,0.2]
for i in indexes:
for g, c, o in zip(groups, colors, offset):
fig.add_trace(
go.Bar(x=df['idx'], y=df[g], marker_color=c, width=width, offset=o)
)
fig.update_traces(
marker_line_color='rgb(8,48,107)',
marker_line_width=1.5,
opacity=0.6
)
fig.update_layout(
xaxis_title='idx',
yaxis_title='value',
barmode='group',
showlegend=False
)
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.