Hey @johann.petrak using the fig.add_vline()
ultimately just adds shapes (a line shape) to a given y-axis. As default value, the y-domain is used.
I donβt think you an actually change that. But knowing that v_lines
are just shapes in a given axis, you might write your custom add_vline()
function:
def add_vlines(figure: go.Figure, x_coordinate:int, y_axis_indicator:str="") -> go.Figure:
figure_dict = figure.to_dict()
existing_shapes = figure_dict.get("layout", {}).get("shapes", [])
new_vline =[{
'type': 'line',
'x0': x_coordinate,
'x1': x_coordinate,
'xref': 'x',
'y0': 0,
'y1': 1,
'yref': f'y{y_axis_indicator} domain'
}]
updated_shapes = existing_shapes + new_vline
figure_dict["layout"]["shapes"] = updated_shapes
return go.Figure(figure_dict)
usage:
import plotly.graph_objects as go
import math
def makefig():
xs = list(range(200))
fig=go.Figure()
fig.add_trace(go.Scatter(mode="lines+markers",x=xs,y=[math.sin(x/10) for x in xs],name="d1", yaxis="y"))
fig.update_layout(yaxis=dict(domain=[0,0.3], showline=True, side="left", autorange=True))
fig.add_trace(go.Scatter(mode="lines+markers",x=xs,y=[math.sin(x/5) for x in xs],name="d2", yaxis="y2"))
fig.update_layout(yaxis2=dict(domain=[0.3,0.6], showline=True, side="right", autorange=True))
fig.add_trace(go.Scatter(mode="lines+markers",x=xs,y=[math.sin(x) for x in xs],name="d3", yaxis="y3"))
fig.update_layout(yaxis3=dict(domain=[0.6,0.9], showline=True, side="left", autorange=True))
fig.update_traces(showlegend=False)
fig.update_layout(height=800)
return fig
fig = makefig()
fig = add_vlines(fig, 22)
fig = add_vlines(fig, 44, "2")
fig = add_vlines(fig, 10, "3")
fig.show()
You might improve the quick&dirty function I came up with. One thing to consider is, that this approach will not work for figures with a single y axis. So itβs pretty much tailored to the example you gave above.
Also keep in mind that the y0
axis does not exist in plotly- Iβve never really understood why
mrep v_line