I want to add a single point / market in a dcc.Graph
after clicking on a random point in the figure. I read the [documentation][1] about interactive graphing in Dash / Plotly. It says the following:
The dcc.Graph component has four attributes that can change through user-interaction: hoverData, clickData, selectedData, relayoutData. These properties update when you hover over points, click on points, or select regions of points in a graph.
I write the following code:
from dash import Dash, html, dcc
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
import plotly.graph_objects as go
app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div([
dbc.Row(
[
dbc.Col([
dcc.Graph(
id='green',
)
],
md=12)
]
)
])
@app.callback(Output('green', 'figure'),
Input('green', 'hoverData'))
def update_graph(hoverData):
if hoverData:
x = hoverData['points'][0]['x']
y = hoverData['points'][0]['y']
z = hoverData['points'][0]['z']
data = [go.Surface(
z=np.zeros((1000, 1000)),
colorscale='algae',
opacity=0.9,
showscale=False
),
go.Scatter3d(
x=[x],
y=[y],
z=[z],
opacity=1,
marker=dict(size=10, color='white')
)
]
return {'data': data}
else:
data = [go.Surface(
z=np.zeros((1000, 1000)),
colorscale='algae',
opacity=0.9,
showscale=False
)]
return {'data': data}
if __name__ == '__main__':
app.run_server(debug=True)
It takes a little bit time, but when you keep your mouse still a white point arises in the Figure. Now I want to lock/freeze the location of this point by a Mouse click event. How can I achieve this?