How to add vertical line in Plotly

In Plotly, you can add a vertical line to a plot by using the “shape” attribute in the layout. Here’s a simple example in Python:

import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'x': [1, 2, 3, 4, 5],
    'y': [1, 4, 9, 16, 25]
})

fig = px.line(df, x='x', y='y')

fig.update_layout(
    shapes=[
        dict(
            type='line',
            x0=3,
            y0=0,
            x1=3,
            y1=30,
            yref='paper',
            line=dict(
                color='red',
                width=2
            )
        )
    ]
)

fig.show()

In this example, we create a line plot using Plotly Express, and then add a vertical line to the plot by updating the layout with a shape dictionary. The x0 and x1 attributes specify the x-coordinates of the line, y0 and y1 specify the y-coordinates of the line, and yref=’paper’ sets the reference system for the y-coordinates. The line attribute controls the color and width of the line.

See also  How to Change the Title Font Size in Plotly