Skip to content

Instantly share code, notes, and snippets.

@saliksyed
Created July 10, 2017 18:50
Show Gist options
  • Save saliksyed/a8c9b7bf52fe690d00838a3d24480320 to your computer and use it in GitHub Desktop.
Save saliksyed/a8c9b7bf52fe690d00838a3d24480320 to your computer and use it in GitHub Desktop.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import json
# create new figure, axes instances.
# we specify specific sizes for the figure to make it bigger
# DPI is "Dots per inch" -- it's used in the printing world
# The reason we specify the DPI instead of raw pixels is because
# we want the text to be consistently scaled.
my_dpi = 75
fig = plt.figure(figsize=(3600/my_dpi, 1800/my_dpi), dpi=my_dpi)
m = Basemap()
m.drawcoastlines()
data = json.loads(open("africa_china_airports.json","r").read())
lats = []
lons = []
for airport_id in data:
if data[airport_id]["country"] == "China":
lats.append(data[airport_id]["latitude"])
lons.append(data[airport_id]["longitude"])
# add scatter points for China only to the graph
m.scatter(lons, lats, latlon=True, marker="D",color="r")
lats = []
lons = []
populations = []
country_data= json.loads(open("africa_country_data.json","r").read())
for airport_id in data:
country = data[airport_id]["country"]
if country != "China":
lats.append(data[airport_id]["latitude"])
lons.append(data[airport_id]["longitude"])
populations.append(country_data[country]["population"])
# add scatter points for Africa only to the graph
m.scatter(lons, lats, latlon=True, marker="o",c=populations, cmap='coolwarm')
route_data = json.loads(open("africa_china_routes.json","r").read())
for route in route_data:
if route_data[route]["to_china"]:
a1 = route_data[route]["airport_id_1"]
a2 = route_data[route]["airport_id_2"]
start_airport_data = data[a1]
end_airport_data = data[a2]
lat1 = start_airport_data["latitude"]
lng1 = start_airport_data["longitude"]
lat2 = end_airport_data["latitude"]
lng2 = end_airport_data["longitude"]
m.drawgreatcircle(lng1, lat1, lng2, lat2, color="green")
plt.savefig('map.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment