Alright, here you are:
import dash
import plotly.graph_objects as go
import plotly.express as px
from dash import Dash, dcc, html, Output, Input, State, callback, dcc, html
import pandas as pd
import json
app = Dash()
df = pd.read_csv(r'https://estrategis-ec.com/data_dash/data.csv', delimiter=';',encoding = 'ISO-8859-1')
def fig_map(df):
fig = px.scatter_mapbox(df, lat="lat", lon="lon",
height=600, width=900,
color="Empresas", size="Empresas", size_max=75, animation_frame='year',
color_continuous_scale='viridis',
mapbox_style="carto-positron", zoom=5.5, hover_name="City",
center={"lat": -2, "lon": -79.01})
return fig
app.layout = html.Div([
html.Div([
dcc.Graph(id='map_plot1', figure=fig_map(df))],
style={'width': '75%', 'display': 'inline-block'}),
html.Div([
dcc.Graph(figure={}, id='my-graph',
style={'width': '25%', 'display': 'inline-block'})
])])
##calback
@callback(
Output(component_id='my-graph', component_property='figure'),
Input(component_id='map_plot1', component_property='clickData'),
State('map_plot1', 'figure'),
prevent_initial_call=True
)
def update_graph(clickData, f):
if clickData:
i = clickData['points'][0]['pointNumber']
city = f['data'][0]['hovertext'][i]
dff = df[df['City'] == city]
title = '<b>{}</b><br>'.format(city)
fig = px.line(dff, x='year', y='Empresas', title=f'{title}')
return fig
return dash.no_update
if __name__ == '__main__':
app.run_server(debug=True, port=8059)
Now, the reason for the changes.
clickData pointNumber represents the data point that you wanted to extract, this is not a list, but an integer. This then needed to be related back to the figure’s data to pull the hovertext (city) from the data, using the point from the first step. This being because you wanted to relate it back to the city instead of using the lat,lon combo to find your city.