Skip to content

Instantly share code, notes, and snippets.

@saliksyed
Created June 21, 2017 16:47
Show Gist options
  • Save saliksyed/ba4e7ccf4b3744d63d70bdcdc0c56447 to your computer and use it in GitHub Desktop.
Save saliksyed/ba4e7ccf4b3744d63d70bdcdc0c56447 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 21 18:01:05 2017
@author: saliksyed
"""
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
# Open the file with all the airports and read every line
data = open("airports.dat", "r").readlines()
# make an array to store the final data
final_data = []
# go through each row and extract the airport country
for row in data:
row_items = row.split(",") # split by the comma
data_dict = {}
data_dict["airport_country"] = row_items[3].decode('utf-8', 'ignore')
final_data.append(data_dict)
# create a count dictionary to hold the results
counts = {}
# loop through the data points
for data_point in final_data:
country_name = data_point["airport_country"]
# if the country is missing (hasn't been seen yet) then initialize it to 0
if not country_name in counts:
counts[country_name] = 0
# add to the count
counts[country_name] += 1
# the categorical values (names)
countries = counts.iterkeys()
# the lengths of the bars
number_of_airports = counts.itervalues()
tmp = sorted(zip(countries, number_of_airports), 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 Airports')
# Sets the chart title
plt.title('Number of Airports by Country')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment