python - 如何向 plotly express 条形图添加一条线

标签 python pandas plotly plotly-dash

我正在尝试在 plotly 中的条形图中添加一条非常简单的线,并且正在为此努力。我的数据框包含一个包含 bins 的列,另一列包含 returnsdata 并且可以在此处复制:

{'bins': {0: '(-23.077, 25.877]', 1: '(25.877, 34.666]', 2: '(34.666, 42.552]', 3: '(42.552, 46.044]', 4: '(46.044, 49.302]', 5: '(49.302, 52.746]', 6: '(52.746, 57.075]', 7: '(57.075, 62.349]', 8: '(62.349, 69.171]', 9: '(69.171, 90.975]'}, 'returns': {0: 0.39754, 1: 0.6817, 2: -0.1918399999999998, 3: -0.44406, 4: -0.6611199999999998, 5: -0.0742857142857142, 6: 0.25304, 7: 0.4166, 8: 0.97648, 9: 0.0539999999999999}}

我使用以下代码从中创建了一个 plotly 条形图:

fig = px.bar(dfs, x='bins', y='returns')
fig.show()

enter image description here

我想在代表基准分数的条形图中添加一条常量线,并查看了这个:Plotly: How to add trendline to a bar chart?

这些方法似乎被贬低了,我似乎找不到任何方法来做到这一点。基准列表是这样的:

[0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858]

我希望它看起来像这样(这条线应该是直的,为可怕的油漆工作编辑道歉)

有人知道怎么做吗?

enter image description here

最佳答案

您可以使用 Plotly 的 horizontal and vertical shapes添加水平线:

fig.add_hline(y=0.14080542857142858)

关于python - 如何向 plotly express 条形图添加一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70173576/

相关文章:

python - 如何创建一个 3D 数组,其中第 k 个元素是一个 2x2 矩阵,我在向量中定义了系数

python - 有多少 Actor 与其他不同 Actor / Actor 合作过

python - Pandas 按列和索引邻接对数据框进行分组

不接受日期时间的 plotly (px) animation_frame 错误

r - 为 R : Remove the k that apears on the y axis when the dataset contains numbers larger than 1000 作图

python - Plotly:如何向烛台图表添加交易量

python - 如何修复将 Python 子进程迁移到 unicode_literals 的编码?

python - 如何在列表中循环更多次 python 中的列表大小?

Python使用pandas每三行转列

python - 在 Python 中查找两个列表/数组中最近的项目