Skip to content

Instantly share code, notes, and snippets.

@saliksyed
Created June 21, 2017 16:32
Show Gist options
  • Save saliksyed/69f8a52654c9b36bd5d927d8dd5caf1d to your computer and use it in GitHub Desktop.
Save saliksyed/69f8a52654c9b36bd5d927d8dd5caf1d 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)
objects = counts.keys()
# y_pos holds the position of the bars ... np.arange(N) just produces N evenly spaced ticks
y_pos = np.arange(len(objects))
# the lengths of the bars
performance = counts.values()
# Plots the bar chart
plt.barh(y_pos, performance, align='center', alpha=0.5)
# Adds the tick labels
plt.yticks(y_pos, objects)
# 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