Reputation: 594
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?
Upvotes: 10
Views: 3304
Reputation: 1275
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:
plotly.js
in your figure:# Assuming fig is your plotly figure
fig.update_traces(hoverinfo="none", hovertemplate=None)
hoverData
attribute of dcc.Graph
that includes the figure and outputs the above described show
, bbox
and children
:# 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
Upvotes: 2