Advertisement
greenmikey

Untitled

Feb 24th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. %matplotlib inline
  2.  
  3. import matplotlib
  4. from matplotlib.patches import Polygon
  5. from matplotlib.collections import PatchCollection
  6. import matplotlib.pyplot as plt
  7.  
  8. from mpl_toolkits.basemap import Basemap
  9. import numpy as np
  10. import pandas as pd
  11.  
  12. #List of Franklin County Zip Codes
  13. ziplist = [43146, 43202,43201,43204,43203,43206,43205,43209,43207,43211,43002,43210,43213,43004,43212,43215,43214,
  14.            43217,43216,43219,43221,43220,43223,43016,43222,43224,43017,43228,43227,43230,43229,43232,43026,43231,
  15.            43235,43236,43268,43054,43068,43081,43085,43109,43110,43119,43125,43123,43126,43137]
  16.  
  17. #Reding in poverty data
  18. df = pd.read_csv('ohio/ACS_15_5YR_S1702_with_ann.csv')
  19.  
  20. #Removing bogus columns - Probably a better way
  21. df = df.drop(df.columns[6:], axis=1)
  22. df = df.drop(df.columns[2:5], axis=1)
  23. df = df.drop(df.columns[0], axis=1)
  24.  
  25. #Renaming the keepers
  26. df.rename(columns={'GEO.id2': 'zipcode','HC02_EST_VC01':'Poverty_Pct'}, inplace=True)
  27.  
  28. #Removing all rows which zipcode key is not in 'ziplist'
  29. df = df[df['zipcode'].isin(ziplist)]
  30.  
  31. #Converting Pverty_Pct to float
  32. df['Poverty_Pct'] = df['Poverty_Pct'].astype(float)
  33.  
  34. df.head()
  35.  
  36. #Make map with 4 corners set in Lat/Lon w/ Mercator projection type
  37. m = Basemap(resolution='c', # c, l, i, h, f or None
  38.             projection='tmerc',
  39.             llcrnrlon = -83.26, llcrnrlat = 39.79,
  40.             urcrnrlon = -82.76, urcrnrlat = 40.175,
  41.             lat_0 = 40, lon_0 = -83)
  42.  
  43. m.readshapefile('cb_2015_us_zcta510_500k','frankzips', zorder=2, color = 'none')
  44. #I get a blank plot. Should this happen?
  45.  
  46. #determine header name with zipcode for matching - 'ZCTA5CE10'
  47. print(m.frankzips_info[1])
  48.  
  49. #Pull shapes matched with zipcodes into dataframe convering zipcode to int.
  50. df_poly = pd.DataFrame({
  51.         'shapes': [Polygon(np.array(shape), True) for shape in m.frankzips],
  52.         'zipcode': [int(zipcode['ZCTA5CE10']) for zipcode in m.frankzips_info]
  53.     })
  54.  
  55. df_poly.head()
  56.  
  57. #Create newframe which merges df and df_poly on zipcode w/ left merge  (keeping only those that match with df zips)
  58. newframe = pd.merge(df, df_poly, on='zipcode', how='left')
  59. newframe.head()
  60.  
  61. #Add bins to dataframe based on poverty %
  62. num_colors = 7
  63. values = newframe['Poverty_Pct']
  64. cmap = plt.get_cmap('Oranges')
  65. scheme = [cmap(i / num_colors) for i in range(num_colors)]
  66. bins = np.linspace(values.min(), values.max(), num_colors)
  67. newframe['bin'] = np.digitize(values,bins) -1
  68. newframe.head()
  69.  
  70. #this is where things go to hell
  71. plt.clf()
  72. fig = plt.figure() #figsize=(22, 12)
  73. ax = fig.add_subplot(111, frame_on=False)
  74.  
  75. patches = []
  76. colors = []
  77. for bin, shape in zip(newframe['bin'], newframe['shapes']):
  78.     colors.append(scheme[bin])
  79.     patches.append(shape)
  80.  
  81.    
  82. pc = PatchCollection(patches)
  83. pc.set_facecolor(colors)
  84. pc.set_edgecolor('black')
  85.  
  86. ax.add_collection(pc)
  87. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement