Hi!
I am trying to create a dash app where a user can add multiple vertical lines to a plot, but it’s an unknown amount. Some plots may have none, others may have 1, 2, 3, etc. I want just one input box that they can click over and over again to add lines. Any thoughts on how to do this? Will I just need a loop inside my callback that listens for this?
TIA!
AIMPED
January 4, 2023, 10:42pm
2
Hi, how do you want the user to fix the position of the vertical line? I suppose you don’t want to have multiple lines at the same place?
One way to do this by clicking into the graph and creating an annotation [or vlines] at the click position.
Here is an example:
from dash import Dash, dcc, html, Input, Output, State
import dash_bootstrap_components as dbc
from dash.exceptions import PreventUpdate
import plotly.express as px
import numpy as np
# create image and plotly express object
img = np.random.randint(0, 255, (90, 160))
fig = px.imshow(img, color_continuous_scale='Blugrn')
# update layout
fig.update_layout(
template='plotly_dark',
plot_bgcolor='rgba(0, 0, 0, 0)',
paper_bgcolor='rgba(0, 0, 0, 0)',
width=700,
height=500,
margin={
'l': 0,
'r': 0,
This file has been truncated. show original
AIMPED
January 4, 2023, 10:46pm
3
This might be interesting too:
Hi @nourma2 ,
I don’t think that there is such a builtin function. You could create a scatterplot with the y0,y1, mode=‘lines’ and this as trace to your figure.
You could also create lines as annotations.
EDIT: I created a function for testing purposes (quick an dirty)
import plotly.graph_objects as go
def add_lines(orientation, levels, bounds):
if orientation == 'h':
traces = [
go.Scatter(x=bounds, y=[level]*2, mode='lines', name=f'{orientation}_line_{idx}')
…