I am trying to plot something similar to the gif below using plotly, where an image is displayed when hovering on a point in the scatter plot, but I just don't know where to start. Is it even possible to use plotly or will I have to use Bokeh?
-
1Do you want to display the image inside the tooltip, or outside/next to the plot? For the former, this Github issue is current tracking progress in adding the feature to Plotly.py: github.com/plotly/plotly.js/issues/1323 For the latter, this Dash app shows how to do it with callbacks: github.com/plotly/dash-sample-apps/tree/main/apps/dash-tsnexhluca– xhluca2021-06-22 17:05:33 +00:00Commented Jun 22, 2021 at 17:05
-
1@xhlulu, thanks for the informative reply. It is indeed the former (inside the tooltip). I'm glad to know that they are currently working on it.Abed Merii– Abed Merii2021-06-23 06:13:23 +00:00Commented Jun 23, 2021 at 6:13
-
Available through dash, see github.com/plotly/plotly.js/issues/1323#issuecomment-933646972Sterling– Sterling2022-06-08 19:23:28 +00:00Commented Jun 8, 2022 at 19:23
Add a comment
|
1 Answer
With Dash 2.0 this can be achieved by the dcc.Tooltip component setting the attributes show (whether the tooltip is shown), bbox (bounding box coordinates) and children (content of the tooltip including the image).
If you take a look at the examples from the dcc.Tooltip documentation, you will notice some key points:
- Turn off the hover effects from native
plotly.jsin your figure:
# Assuming fig is your plotly figure
fig.update_traces(hoverinfo="none", hovertemplate=None)
- The callback is triggered by the
hoverDataattribute ofdcc.Graphthat includes the figure and outputs the above describedshow,bboxandchildren:
# Assuming app is your Dash app
@app.callback(Output("graph-tooltip", "show"), Output("graph-tooltip", "bbox"),
Output("graph-tooltip", "children"), Input("graph-basic-2", "hoverData"),
)
def display_hover(hoverData):
# ...
pt = hoverData["points"][0]
bbox = pt["bbox"]
# ...
# img_src is your image, e.g. from URL or b64 encoded
children = [
html.Div([
html.Img(src=img_src, style={"width": "100%"}),
html.P("Tooltip text"),
])
]
return True, bbox, children
