Hi there,
I’m trying to create a directed graph visualization, which will show the movement of people to different countries during a given year. I am trying to also enable two dropdown filters that will filter the year of the people’s movement and the country they are traveling from (origin_country). Where I am struggling is, I don’t know how to save the user’s dropdown choice into a variable I can use. I had some empty strings I was planning on putting the user’s selection into and then using an if statement when calling the add_trace function to draw the lines (see comments in code). Ideally, I would like the dropdown data to be populated from my travel_paths file (wasn’t sure how to do that either). Any advice on this? Sorry if I’m asking a very basic question, and thanks for any help.
import plotly.graph_objects as go import pandas as pd # to further implement """ 1. be able to filter the data in the future 2. make everything print pretty """ countries = pd.read_csv('https://raw.githubusercontent.com/jerguy1928/refugee_map/master/country_centroid_locations.csv') countries.head() travel_paths = pd.read_csv('https://raw.githubusercontent.com/jerguy1928/refugee_map/master/test_file.csv') travel_paths.head() fig = go.Figure() #adds country centroids fig.add_trace(go.Scattergeo( lon = countries['long'], lat = countries['lat'], hoverinfo = 'text', text= countries['country'], mode = 'markers', marker = dict( size = 2, color = 'rgb(255, 0, 0)', ))) # these 3 variables are currently not used, I was thinking of using them for filtering year = "" origin_country = "" destination_country = "" button_layer_1_height = 1.08 fig.update_layout( updatemenus=[ go.layout.Updatemenu( buttons=list([ dict( args=["year", "1964"], label="1964", method="update", ), dict( args=["year", "1972"], label="1972", method="update", ), dict( args=["year", "1988"], label="1988", method="update", ), dict( args=["year", "1996"], label="1996", method="restyle", ), ]), direction="down", pad={"r": 10, "t": 10}, showactive=True, x=0.1, xanchor="left", y=button_layer_1_height, yanchor="top" ), go.layout.Updatemenu( buttons=list([ #these are some demo dropdown buttons, i would need to dropdown to be populated from values in the countries csv dict( args=["origin_country", "Mozambique"], label="Mozambique", method="update" ), dict( args=["origin_country", "Rwanda"], label="Rwanda", method="update" ), dict( args=["origin_country", "Sudan"], label="Sudan", method="update" ), dict( args=["origin_country", "Cuba"], label="Cuba", method="restyle" ) ]), direction="down", pad={"r": 10, "t": 10}, showactive=True, x=0.37, xanchor="left", y=button_layer_1_height, yanchor="top" ), ] ) for i in range(len(travel_paths)): # if (travel_paths['year'][i] == year) and travel_paths['origin'][i] == origin_country: (Was thinking of using this to filter?) fig.add_trace( go.Scattergeo( lon = [travel_paths['start_lon'][i], travel_paths['end_lon'][i]], lat = [travel_paths['start_lat'][i], travel_paths['end_lat'][i]], mode = 'lines+markers', line = dict(width = 1,color = 'red'), marker= dict( size = 5, symbol = 'triangle-down' ), hoverinfo = 'text', text= travel_paths['origin'][i] + " -> " + travel_paths['destination'][i] ) ) fig.update_layout( title_text = 'Movement of Refugees UNHCR <br> Choose source country from the list <br>', showlegend = False, geo = go.layout.Geo( scope = 'world', projection_type = 'mercator', showland = True, showcountries= True, landcolor = 'rgb(243, 243, 243)', countrycolor = 'rgb(204, 204, 204)', ), ) fig.show()