✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Returning mpl_toolkit.basemap figure to Dash

Hello,

I have a doubt on how I can return to the dash layout through a callback the graph of a network plotted over mpl_toolkit.basemap graph.

I have network with the airports and airplanes routes in a df called “df_merged” that looks like the following:

I’m plotting on a worldmap with the following code:

def create_graph_object(df, directionality):

  • graph = nx.from_pandas_edgelist(df, source = ‘source airport’, *
  •                             target = 'destination airport', create_using = directionality)*
    
  • return graph *

def create_pos_variable(df, m):

  • Assign the longitude to mx and the latitude to my*

  • Because you assign it to the m, which is the basemap, the coordinates are *

  • recalculated to the size of m*

  • mx, my = m(df[‘longitude’].values, df[‘latitude’].values)*
  • pos = {}*
  • for count, elem in enumerate (df[‘source airport’]):*
  •     pos[elem] = (mx[count], my[count])*
    
  • now the parameters G (the graph) and pos (the positions) are set *

  • return pos*

def draw_nodes_and_edges(graph, pos, node_size, node_visibility, edge_visibility, ncolor=’#F7A538’, ecolor=’#5EC4B7’, ewidth = 2):

  • draw the nodes of graph on the map and set other parameters for layout *

  • nx.draw_networkx_nodes(graph, pos, node_size = node_size, node_color = ncolor, alpha = node_visibility)*
  •                 *
    
  • draw the edges of graph on the map and set other parameters for layout*

  • nx.draw_networkx_edges(graph, pos, edge_color = ecolor, width = ewidth, alpha = edge_visibility)*
  •    *
    

dataframe=df_merged
directionality=nx.Graph()
node_size=20
hub_nr=0
node_visibility=0.8
edge_visibility=0.1

# create graph object from dataframe
graph = create_graph_object(dataframe, directionality)

# draw mercator projection as background and set size
plt.figure(figsize = (15,20))
m = Basemap(projection=‘merc’,

  •        llcrnrlon=-180,*
    
  •        llcrnrlat=-80,*
    
  •        urcrnrlon=180,*
    
  •        urcrnrlat=80)*
    

# include coastlines, countries and boundaries
m.drawcoastlines()
m.drawmapboundary()
m.drawcountries()

# include longitude and latitude lines if you want
m.drawparallels(np.arange(-90,90,30))
m.drawmeridians(np.arange(-180,180,60))

# create variable pos, that contains the position of each node
*pos = create_pos_variable(dataframe, m) *

# draw the nodes and edges on the map and set other parameters for layout
draw_nodes_and_edges(graph, pos, node_size, node_visibility, edge_visibility)

# if hub_nr is more than 0, draw biggest hubs
if hub_nr > 0:

  • draw_biggest_hubs(dataframe, hub_nr, graph, pos, ‘#CC0000’)*

  • find biggest hubs and draw hub labels*

  • hub_table = setair.find_hubs_in_df(dataframe, hub_nr)*

  • hub_network_labels(hub_table, graph, pos)*

# show plot
plt.show()

And this is the result:

How could I include this code in a callback function that returns this graph into a dash layout?

Thank you very much,

Source:https://github.com/JaapVanDerAar/Flight-Network-Analysis (here is the source of the original code I am using)

def create_graph_object(df, directionality):
graph = nx.from_pandas_edgelist(df, source = ‘source airport’,
target = ‘destination airport’, create_using = directionality)
return graph

def create_pos_variable(df, m):

# Assign the longitude to mx and the latitude to my
# Because you assign it to the m, which is the basemap, the coordinates are 
# recalculated to the size of m
mx, my = m(df['longitude'].values, df['latitude'].values)
pos = {}
for count, elem in enumerate (df['source airport']):
     pos[elem] = (mx[count], my[count])
# now the parameters G (the graph) and pos (the positions) are set  
return pos

def draw_nodes_and_edges(graph, pos, node_size, node_visibility, edge_visibility, ncolor=’#F7A538’, ecolor=’#5EC4B7’, ewidth = 2):

# draw the nodes of graph on the map and set other parameters for layout     
nx.draw_networkx_nodes(graph, pos, node_size = node_size, node_color = ncolor, alpha = node_visibility)
                 
# draw the edges of graph on the map and set other parameters for layout
nx.draw_networkx_edges(graph, pos, edge_color = ecolor, width = ewidth, alpha = edge_visibility)

dataframe=df_merged
directionality=nx.Graph()
node_size=20
hub_nr=0
node_visibility=0.8
edge_visibility=0.1

create graph object from dataframe

graph = create_graph_object(dataframe, directionality)

draw mercator projection as background and set size

plt.figure(figsize = (15,20))
m = Basemap(projection=‘merc’,
llcrnrlon=-180,
llcrnrlat=-80,
urcrnrlon=180,
urcrnrlat=80)

include coastlines, countries and boundaries

m.drawcoastlines()
m.drawmapboundary()
m.drawcountries()

include longitude and latitude lines if you want

m.drawparallels(np.arange(-90,90,30))
m.drawmeridians(np.arange(-180,180,60))

create variable pos, that contains the position of each node

pos = create_pos_variable(dataframe, m)

draw the nodes and edges on the map and set other parameters for layout

draw_nodes_and_edges(graph, pos, node_size, node_visibility, edge_visibility)

if hub_nr is more than 0, draw biggest hubs

if hub_nr > 0:
draw_biggest_hubs(dataframe, hub_nr, graph, pos, ‘#CC0000’)

# find biggest hubs and draw hub labels
hub_table = setair.find_hubs_in_df(dataframe, hub_nr)
hub_network_labels(hub_table, graph, pos)

show plot

plt.show()