我正在尝试在 plotly
中的条形图中添加一条非常简单的线,并且正在为此努力。我的数据框包含一个包含 bins
的列,另一列包含 returns
data 并且可以在此处复制:
{'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()
我想在代表基准分数的条形图中添加一条常量线,并查看了这个: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]
我希望它看起来像这样(这条线应该是直的,为可怕的油漆工作编辑道歉)
有人知道怎么做吗?
最佳答案
您可以使用 Plotly 的 horizontal and vertical shapes添加水平线:
fig.add_hline(y=0.14080542857142858)
关于python - 如何向 plotly express 条形图添加一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70173576/