Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Jun 26 12:36:35 2017
  5.  
  6. @author: saliksyed
  7. """
  8.  
  9. import matplotlib.pyplot as plt
  10. import numpy as np
  11. import matplotlib.pyplot as plt
  12.  
  13.  
  14. # Let's read in the countries for each airport:
  15.  
  16. data = open("airports.dat", "r").readlines()
  17.  
  18. final_data = []
  19.  
  20. for row in data:
  21. row_items = row.split(",") # split by the comma
  22. data_dict = {}
  23. data_dict["airport_country"] = row_items[3].decode('utf-8', 'ignore')
  24. data_dict["altitude"] = float(row_items[8])
  25. final_data.append(data_dict)
  26.  
  27. # now let's keep a count of each countries
  28.  
  29. counts = {}
  30. for data_point in final_data:
  31. country_name = data_point["airport_country"]
  32. altitude = data_point["altitude"]
  33. if not country_name in counts:
  34. counts[country_name] = []
  35. counts[country_name].append(altitude)
  36.  
  37. averages = {}
  38. for country in counts:
  39. averages[country] = np.average(counts[country])
  40.  
  41.  
  42. fig, ax = plt.subplots()
  43.  
  44. #fig = the figure containing the visualization
  45. # ax = the axes that are attached to the visualization
  46.  
  47. # Get the countries and their counts using hte map
  48. countries = averages.keys()
  49. airport_counts = averages.values()
  50.  
  51. #### HERE is the magic:
  52. # I'll break it down step by step
  53. # zip() takes two arrays and merges the elements
  54. # so if you had zip(['a','b','c'], [1,2,3]) you would get a new array
  55. # [('a',1), ('b', 2), ('c', 3)]
  56. # pretty cool right?
  57. # now we sort the zipped array using a lambda. The lambda tells the sort algorithm
  58. # how it should choose the value to sort by (what the "key" is). in this case we want to sort by the airport count
  59. # this airport count is the second element so we say key=lambda x : x[1]. Finally we specify reverse=True
  60. # because we want the highest valued airports 1st. Finally we just pick the top 25!
  61.  
  62. final_data = sorted(zip(countries,airport_counts), key=lambda x : x[1], reverse=True)[:25]
  63.  
  64. # now we have the final data but it's in zipped format so we breka it back up into individual arrays:
  65. countries = [x[0] for x in final_data]
  66. airport_counts = [x[1] for x in final_data]
  67.  
  68.  
  69. # "arange" returns an evenly spaced interval of the specified length
  70. y_pos = np.arange(len(airport_counts))
  71.  
  72.  
  73. # add bars evenly spaced according to y_pos. The length of the bar should be the count of the airports
  74. ax.barh(y_pos, airport_counts, align='center',
  75. color='green')
  76.  
  77. # add ticks to the axes
  78. ax.set_yticks(y_pos)
  79.  
  80. # add a label to the axis with the name of each country
  81. ax.set_yticklabels(countries)
  82.  
  83.  
  84. ax.invert_yaxis() # labels read top-to-bottom
  85.  
  86. # set the x axis label
  87. ax.set_xlabel('Average altitude')
  88.  
  89. # set the chart title
  90. ax.set_title('Countries with the highest average altitude of airports')
  91.  
  92. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement