python - Plotly:向条形图添加阴影阈值
问题描述
我在 Pandas 数据框中有一个销售数据集,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
我绘制了这些数据,如下所示:
import plotly.express as px
fig = px.bar(df, x='date', y='sales')
fig.show()
我希望能够添加一些阴影“阈值”,类似于以下内容:
阈值是:
- 绿色:大于 5,000
- 黄色:大于 2,200 且小于 5,000
- 红色:小于 2,200
这将类似于 中的fill_between
方法Matplotlib
。
这在 Plotly 中可能吗?
谢谢!
解决方案
最好是add_hrect()
在这种情况下使用。
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)
推荐阅读
- html - 使 div 覆盖完全匹配视频 div
- python - 在 ast.ImportFrom 中查找 end_lineno
- android - 添加 Gradle 依赖关系会破坏 Gradle 构建
- python - Python 新手。有人可以向我解释这行代码吗?
- audio - 您可以在 Autohotkey 中录制音频或系统音频吗?
- text - 尝试识别类似于 ruby 注释的文本降价
- vba - 仅在选定组件中应用 MACRO (swModel.GetComponents)
- javascript - 有人可以帮我 TypeError: Cannot read property 'map' of undefined
- dns - Cloudflare 将域转换为 Cloudflare 公共 IP,而不是我的服务器公共 IP
- airtable - Airtable 脚本块 - 将一个字段批量复制到同一张表中的另一个字段(10,000 条记录)