Distribution network visualization (Python)

One interesting domain of prescriptive analytics is network design and optimization, e.g. a hub-and-spoke distribution network. Optimal facility and capacity allocation is one example of this. Optimal flow design is another example: Where to source, where to store, and how to route to customers? Christian (https://cdruf.com/) demonstrated in his SCDA article how operations research is applied to supply chain network design. You can read his article here:

Here I provide a template for plotting a distribution network in Python.

Python code for distribution network visualization

Here is a sample code that demonstrated how you can visualize a hub supplying various spokes in a distribution network.

import folium

# set the center of the map to the USA
center_location = [39.8283, -98.5795]
# set the initial zoom level
zoom_level = 4

# create a map object
map = folium.Map(location=center_location, zoom_start=zoom_level)

# add a marker for the hub
hub_location = [41.8781, -87.6298]
folium.Marker(location=hub_location, icon=folium.Icon(color='blue')).add_to(map)

# add markers for the spokes
spoke_locations = [[37.7749, -122.4194], [34.0522, -118.2437], [29.7604, -95.3698], [40.7128, -74.0060]]
for location in spoke_locations:
    folium.Marker(location=location, icon=folium.Icon(color='red')).add_to(map)

# add lines connecting the hub to the spokes
for location in spoke_locations:
    folium.PolyLine(locations=[hub_location, location], color='black').add_to(map)

# save the map to an HTML file
map.save('hub_spoke_map.html')

This generates the following plot:

visualizing distribution network in python

This is not really a hub-and-spoke network as most SCM analysts would see it. The benefit of a hub-and-spoke network is that you can ship larger volumes between hubs, and then deliver smaller volumes only on the last miles to the end customer (spokes). FedEx, for example, has several hubs across the US (e.g. airports), with large concentrated volumes transported between hubs and then smaller volumes distributed to customers from there. So, I adjust the code a bit to add some more spokes and another hub.

import folium
import pandas as pd

# create the data for hubs and spokes
data = {'Type': ['Hub', 'Hub', 'Spoke', 'Spoke', 'Spoke', 'Spoke', 'Spoke', 'Spoke', 'Spoke', 'Spoke', 'Spoke', 'Spoke', 'Spoke', 'Spoke', 'Spoke'],
        'Latitude': [30.8781, 40.7749, 34.0522, 29.7604, 40.7128, 41.8781, 37.7749, 34.0522, 29.7604, 47.6062, 45.5231, 43.6532, 39.7392, 38.9072, 32.7767],
        'Longitude': [-87.6298, -122.4194, -118.2437, -95.3698, -74.0060, -87.6298, -122.4194, -118.2437, -95.3698, -122.3321, -122.6765, -79.3832, -104.9903, -77.0369, -96.7970]}

# create a DataFrame from the data
df = pd.DataFrame(data)

# set the center of the map to the USA
center_location = [39.8283, -98.5795]
# set the initial zoom level
zoom_level = 4

In above example I adjusted the hub locations a bit so that they are easier to see on the map. Here is what the plot looks like now:

distribution network visualization in python

This plot currently displays all possible delivery routes to all spokes from a given hub. You could further adjust this plot to e.g only draw lines to the spokes actually supplied by the given hub, and also a line from hub to hub.

Content related to Python distribution network visualization

Consider reading the following SCDA articles if you want to learn more about Leaflet, folium, or spatial data visualization in general:

You May Also Like

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.