[英]Plotly: preventing bars on bar chart from changing color based on background color
I have a set of sales data in a Pandas dataframe df
that looks similar to the following:我在 Pandas dataframe
df
中有一组销售数据,看起来类似于以下内容:
import pandas as pd
df = pd.DataFrame({'date':['2021-01-04', '2021-01-05', '2021-01-06', '2021-01-07', '2021-01-08', '2021-01-11', '2021-01-12', '2021-01-13', '2021-01-14', '2021-01-15'],
'sales':[1500, 2000, 1300, 2700, 1800, 4500, 2600, 2750, 8000, 1300]})
date sales
0 2021-01-04 1500
1 2021-01-05 2000
2 2021-01-06 1300
3 2021-01-07 2700
4 2021-01-08 1800
5 2021-01-11 4500
6 2021-01-12 2600
7 2021-01-13 2750
8 2021-01-14 8000
9 2021-01-15 1300
I then plot this data, as follows:我接着plot这个数据,如下:
import plotly.express as px
fig = px.bar(df, x='date', y='sales')
fig.add_hrect(y0=0, y1=2200, line_width=0, fillcolor="red", opacity=0.25)
fig.add_hrect(y0=2200, y1=5000, line_width=0, fillcolor="yellow", opacity=0.25)
fig.add_hrect(y0=5000, y1=10000, line_width=0, fillcolor="green", opacity=0.25)
fig.show()
As you can see, the blue vertical bars turn a purple color when combined with the red background color.如您所见,蓝色垂直条与红色背景颜色结合时变为紫色。
One workaround might be to paint the red, green, and yellow background colors first, then to overlay the blue bars.一种解决方法可能是先绘制红色、绿色和黄色背景 colors,然后覆盖蓝色条。 This way, we're not 'mixing' red and blue, thereby making purple.
这样,我们就不会“混合”红色和蓝色,从而产生紫色。
Is this possible?这可能吗? Is there another, better solution?
还有其他更好的解决方案吗?
Thanks!谢谢!
There is the option to add layer='below'
to the add_hrect
.可以选择将
layer='below'
添加到add_hrect
。
fig = px.bar(df, x='date', y='sales')
fig.add_hrect(y0=0, y1=2200, line_width=0, fillcolor="red", opacity=0.25, layer='below')
fig.add_hrect(y0=2200, y1=5000, line_width=0, fillcolor="yellow", opacity=0.25, layer='below')
fig.add_hrect(y0=5000, y1=10000, line_width=0, fillcolor="green", opacity=0.25, layer='below')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.