In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from mpl_toolkits.basemap import Basemap
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
colors = ['#fff5eb','#fee6ce','#fdd0a2','#fdae6b','#fd8d3c','#f16913','#d94801','#a63603','#7f2704']
transit = pd.read_csv(r"datasets/ACS_15_5YR_B08101-4 DCMDVA/ACS_15_5YR_B08101.csv")
transit["Drive_Pct"] = (transit["HD01_VD10"] + transit["HD01_VD18"])/transit["HD01_VD01"]
transit = transit.set_index("GEO.id")
transit.head()
Out[1]:
In [2]:
transit.hist("Drive_Pct")
plt.show()
In [3]:
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, axisbg='w', frame_on=False)
m = Basemap(projection='merc',llcrnrlat=38.7,urcrnrlat=39.15,\
llcrnrlon=-77.4,urcrnrlon=-76.8,resolution='c')
m.readshapefile(r"datasets/gz_2010_11_140_00_500k/gz_2010_11_140_00_500k",'dc',drawbounds = False)
m.readshapefile(r"datasets/gz_2010_24_140_00_500k/gz_2010_24_140_00_500k",'md',drawbounds = False)
m.readshapefile(r"datasets/gz_2010_51_140_00_500k/gz_2010_51_140_00_500k","va",drawbounds = False)
Out[3]:
In [4]:
for state in [zip(m.dc_info,m.dc),zip(m.va_info,m.va),zip(m.md_info,m.md)]:
for info, shape in state:
geo_id = info["GEO_ID"]
if geo_id not in transit.index:
color = "#000000"
else:
drive_pct = transit.loc[geo_id,"Drive_Pct"]
if drive_pct > 0.9:
color = colors[8]
elif drive_pct > 0.8:
color = colors[7]
elif drive_pct > 0.7:
color = colors[6]
elif drive_pct > 0.6:
color = colors[5]
elif drive_pct > 0.5:
color = colors[4]
elif drive_pct > 0.4:
color = colors[3]
elif drive_pct > 0.3:
color = colors[2]
elif drive_pct > 0.2:
color = colors[1]
else:
color = colors[0]
patches = [Polygon(np.array(shape), True)]
pc = PatchCollection(patches)
pc.set_facecolor(color)
ax.add_collection(pc)
In [5]:
labels = ["<20%",">20%",">30%",">40%",">50%",">60%",">70%",">80%",">90%"]
for i in range(0,len(labels)):
m.plot(0,0,color=colors[i],label=labels[i])
In [6]:
m.readshapefile(r"datasets/DC Metro Lines/Metro_Lines_Regional", "metro", linewidth=2, color="blue")
# m.readshapefile(r"datasets/interstates/in101503", "highways", linewidth=2, color="grey")
plt.legend(loc=2)
plt.show()
In [ ]: