Ultimately I want a scatter plot shown on the left and an image shown on the right. When the mouse is hovered over a data point in the scatter plot, I want the image shown on the right to be updated. There are probably a few ways to go about doing this. My approach, which is probably not the best way, is to show the image as a background image in a “figure” and hide the axis lines, grid, and tick marks (which I have not implemented in my code just yet). As it stands, the scatter plot is displayed fine, however, the background image is not seen. The issue (as best I can tell) is not related to the image being off-screen. Below is my script. If there is a better way, I would love to hear it.
import dash
import dash_core_components as dcc
from dash.dependencies import Input, Output
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
import base64
app = dash.Dash()
app.css.append_css({'external_url': 'https://codepen.io/illsci/pen/wxZMQm.css'})
df = pd.read_csv(r'test.csv')
y_data = 'ER'
x_data = 'channels'
app.layout = html.Div(className="row", children=[
html.Div([
dcc.Graph(
id='scatter',
figure={
'data': [{
'x': df[x_data],
'y': df[y_data],
'customdata': df['filename'],
'mode': 'markers',
'marker': {
'size': 12,
'color': 'rgba(0, 116, 217, 0.5)',
'line': {
'color': 'rgb(0, 116, 217)',
'width': 0.5
}
}
}],
'layout': {
'xaxis': {
'title': x_data,
},
'yaxis': {
'title': y_data,
},
'margin': {
'l': 50,
'r': 10,
't': 80,
'b': 50
},
'title':"Peak Position vs. ER",
'hovermode': 'closest'
}
},
hoverData={'points': [{'customdata': 'test.mca'}]}
)
], className="six columns"),
html.Div([
dcc.Graph(id='image',
)
], className="six columns")
])
image_route = 'C:/users/test/'
def get_image (encoded_image):
return{
'data': [go.Scatter(
x=[0,1000],
y=[0,1000],
)],
'layout':{
'height': 0,
'margin': {'l': 50, 'b': 30, 'r': 10, 't': 10},
'images': {
'source': encoded_image,
'xref': 'x',
'yref': 'y',
'x':0,
'y':500,
'sizex':200,
'sizey':200,
'sizing': 'contain',
'visible': True,
'layer': 'above'
}
}}
@app.callback(
dash.dependencies.Output('image', 'figure'),
[dash.dependencies.Input('scatter', 'hoverData')])
def update_plot_timeseries(hoverData):
filename = hoverData['points'][0]['customdata']
image_name=filename+".png"
location = image_route + image_name
with open('%s' %location, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode()
encoded_image = "data:image/png;base64," + encoded_string
return get_image (encoded_image)
if __name__ == '__main__':
app.run_server(debug=False, port=8090)