Created
July 4, 2017 21:36
-
-
Save saliksyed/4cb86fd2103e6de400a4a23048104bd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import numpy as np | |
import matplotlib.pyplot as plt | |
graph_data = json.loads(open("routes.json").read()) | |
totals = {} | |
for start_country in graph_data: | |
data = graph_data[start_country] | |
# Get the sum of the number of routes to this country and store it | |
totals[start_country] = sum(data["to"].values()) | |
# From here we can use the same code we saw in Lesson 1 : http://saliksyed.com/index.php/getting-started-with-basic-data-visualization-charting/ | |
# the categorical values (names) | |
countries = totals.iterkeys() | |
# the lengths of the bars | |
number_of_routes = totals.itervalues() | |
tmp = sorted(zip(countries, number_of_routes), key=lambda x : x[1], reverse=True)[:25] | |
countries = [x[0] for x in tmp] | |
number_of_airports = [x[1] for x in tmp] | |
# y_pos holds the position of the bars ... np.arange(N) just produces N evenly spaced ticks | |
y_pos = np.arange(len(countries)) | |
# Plots the bar chart | |
plt.barh(y_pos, number_of_airports, align='center', alpha=0.5) | |
# Adds the tick labels | |
plt.yticks(y_pos, countries) | |
# Sets the X label | |
plt.xlabel('Number of Routes') | |
# Sets the chart title | |
plt.title('Number of Arriving Routes by Country') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment