简体   繁体   English

如何在 plotly 快递条形图中添加一条线

[英]How to add a line to a plotly express bar chart

I am trying to add a very simple line across a bar chart in plotly and am struggling to to this.我正在尝试在plotly的条形图上添加一条非常简单的线,并且正在为此努力。 My dataframe contains one column with bins and the other with returns data and can be copied here:我的 dataframe 包含一列带bins ,另一列带有returns数据,可以在此处复制:

{'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}}

I have created a plotly bar chart from this using the following code:我使用以下代码从这里创建了一个 plotly 条形图:

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

在此处输入图像描述

I want to add a constant line across the bar chart that represents a benchmark score and have looked at this: Plotly: How to add trendline to a bar chart?我想在条形图上添加一条代表基准分数的恒定线,并查看了这个: Plotly:如何将趋势线添加到条形图?

The methods seem to be depreciated and I can't seem to find any way to do this.这些方法似乎被贬低了,我似乎找不到任何方法来做到这一点。 The benchmark list is this:基准清单是这样的:

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

I want it to look this this (the line is suppose to be straight, apologies for the terrible paint job edit)我希望它看起来像这样(这条线应该是直的,为糟糕的油漆工作编辑道歉)

Would anyone know how to do this?有人知道该怎么做吗?

在此处输入图像描述

You can use the Plotly's horizontal and vertical shapes to add a horizontal line:您可以使用 Plotly 的水平和垂直形状添加水平线:

fig.add_hline(y=0.14080542857142858)

I would simply use:我会简单地使用:

fig.add_traces(go.Scatter(x= dfs.bins, y=dfs.benchmark, mode = 'lines'))

Plot Plot

在此处输入图像描述

Complete code:完整代码:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
dfs = pd.DataFrame({'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}})
dfs['benchmark'] = [0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858, 0.14080542857142858]

fig = px.bar(dfs, x='bins', y='returns')
fig.add_traces(go.Scatter(x= dfs.bins, y=dfs.benchmark, mode = 'lines'))
fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM