简体   繁体   English

"Plotly:如何在折线图中添加水平线?"

[英]Plotly: How to add a horizontal line to a line graph?

I made a line graph with the code below and I'm trying to add a horizontal line at y=1.我用下面的代码做了一个折线图,我试图在 y=1 处添加一条水平线。 I tried following the instructions on the plotly site but it is still not showing.我尝试按照情节站点上的说明进行操作,但仍未显示。 Does anyone know why?有谁知道为什么?

date = can_tot_df.date
growth_factor = can_tot_df.growth_factor

trace0 = go.Scatter(
            x=date,
            y=growth_factor,
            mode = 'lines',
            name = 'growth_factor'
)

fig = go.Figure()
fig.add_shape(
        type='line',
        x0=date.min(),
        y0=1,
        x1=date.max(),
        y1=1,
        line=dict(
            color='Red',
        )
)


data = [trace0]
iplot(data)

It's hard to tell exactly what's wrong without a sample of your data.如果没有您的数据样本,就很难确切地说出问题所在。 What I can tell for sure is that you're missing the arguments xref and yref to specify that the line is drawn as units of your y and x axis.可以肯定的是,您缺少参数xrefyref来指定将线绘制为yx轴的单位。 Judging by your sample code, this is what you'd like to do since you're specifying your x-values in terms of dates.从您的示例代码来看,这就是您想要做的,因为您要根据日期指定 x 值。

Also, you don't need to worry about iplot for newer versions of plotly.此外,你不需要操心iplot为plotly新版本。 You can display your chart just as easily by just running fig.show() .您只需运行fig.show()轻松显示图表。 The figure and code sample below will show you how to use fig.show() and how to define your lines in terms of axis units.下面的图形和代码示例将向您展示如何使用fig.show()以及如何根据轴单位定义线条。

Plot:阴谋:

在此处输入图片说明

Code:代码:

import plotly.graph_objects as go
import numpy as np

x = np.arange(10)

fig = go.Figure(data=go.Scatter(x=x, y=x**2))

fig.add_shape(type='line',
                x0=0,
                y0=40,
                x1=8,
                y1=40,
                line=dict(color='Red',),
                xref='x',
                yref='y'
)


fig.show()

An alternative to xref='x' is xref='paper' . xref='x'的替代方法是xref='paper' Now you can specify x0 as a float between 0 and 1 spanning from the start and end of the plot.现在,您可以将x0指定为从绘图开始和结束的01之间的浮点数。

df = df
fig = px.scatter(df, x="date", y="growth_factor", mode = 'lines',
      hover_name=df['growth_factor'] )

fig.update_layout(shapes=[
dict(
  type= 'line',
  yref= 'y', y0= 1, y1= 1,   # adding a horizontal line at Y = 1
  xref= 'paper', x0= 0, x1= 1
     ) 
])

fig.show()图.show()

You're adding the line to your fig object, but fig is not getting passed into the iplot() function, only your data .iplot()条线添加到您的fig对象,但fig没有被传递到iplot()函数,只有您的data So only the trace is getting plotted.所以只有跟踪被绘制。

If you're using a late version of plotly, the new syntax allows you to create this plot simply using the fig object, like:如果您使用的是后期版本的 plotly,新的语法允许您简单地使用fig对象创建此fig ,例如:

from plotly import graph_objects as go

fig = go.Figure()

# Contrived dataset for example.
x = [1, 2, 3, 4]
y = [i**2 for i in x]

fig.add_trace(go.Scatter(
              x=x,
              y=y,
              mode = 'lines',
              name = 'growth_factor'))

fig.add_shape(type='line',
              x0=min(x),
              y0=5,
              x1=max(x),
              y1=5,
              line=dict(color='Red'))

fig.update_shapes(dict(xref='x', yref='y'))

fig.show()

Here are the plotly docs for convenience.为方便起见,这里是plotly文档。

If you use subplots, then this is the easiest way I found to add an other line to a subplot.如果您使用子图,那么这是我发现向子图添加另一行的最简单方法。 this example draws a horizontal line at y=80 for all x values此示例在 y=80 处为所有 x 值绘制一条水平线

from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=1,
                    shared_xaxes=True,
                    vertical_spacing=0.02)
[some graph]

fig.add_trace(go.Scatter(
        name='Y=80',
        x = [df['date'].min(), df['date'].max()],
        y = [80, 80],
        mode = "lines",
        marker = dict(color = 'rgba(80, 26, 80, 0.8)')
    ),row=1, col=1)

i found the solution on github :我在github上找到了解决方案:

You could also use fig.add_hline(y=1) --> see https://plotly.com/python/horizontal-vertical-shapes/您也可以使用fig.add_hline(y=1) --> 参见https://plotly.com/python/horizo​​ntal-vertical-shapes/

import plotly.graph_objects as go
import numpy as np

x = np.arange(10)

fig = go.Figure(data=go.Scatter(x=x, y=x**2))
fig.add_hline(y=40, line_width=3, line_dash="dash", line_color="green")


fig.show()

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

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