forked from aloverso/DataScience16CTW
/
makeplots.py
191 lines (146 loc) · 5.85 KB
/
makeplots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import matplotlib.cbook as cbook
import matplotlib.pyplot as plt
import pandas
from mpl_toolkits.basemap import Basemap
import numpy as np
from matplotlib.colors import rgb2hex
from matplotlib.patches import Polygon
def plot_clinic_locations():
fig = plt.figure(figsize=(20,10))
# define the map
m = Basemap(llcrnrlon=-145.5,llcrnrlat=1.,urcrnrlon=-2.566,urcrnrlat=46.352,\
rsphere=(6378137.00,6356752.3142),\
resolution='l',area_thresh=1000.,projection='lcc',\
lat_1=50.,lon_0=-107.)
# # Draw the coastlines on the map
m.drawcoastlines()
# # Draw country borders on the map
m.drawcountries()
# Fill the land with grey
m.fillcontinents(color = '#888888', zorder=0)
# Draw the map boundaries
m.drawmapboundary(fill_color='#f4f4f4')
# Define our longitude and latitude points
# We have to use .values because of a wierd bug when passing pandas data
# to basemap.
#x,y = m(cities['lng'].values, cities['lat'].values)
# Plot them using round red markers of size 6
for lat in data.latitude:
lon = data.longitude[data.latitude==lat].iloc[0]
x,y = m(lon, lat)
m.plot(x, y, marker='o', color='r', markersize=6)
# Show the map
plt.show()
def get_maps():
map_lower_48 = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49,
projection='lcc',lat_1=33,lat_2=45,lon_0=-95)
states_shp_info = map_lower_48.readshapefile('shapefiles/st99_d00', 'states',drawbounds=True)
m_alaska = Basemap(llcrnrlon=-173,llcrnrlat=51,urcrnrlon=-134.45,urcrnrlat=71.34,
projection='lcc', lat_0=64, lon_0=-151)
alaska_shp_info = m_alaska.readshapefile('shapefiles/st99_d00', 'states',drawbounds=True)
m_hawaii = Basemap(#llcrnrlon=-161,llcrnrlat=21,urcrnrlon=-154,urcrnrlat=22,
width=800000, height=500000,
projection='lcc',
lat_0=20.525, lon_0=-157.385,
lon_1=-158.115, lat_1=17.24,
lon_2=-153.479, lat_2=19.663,
)
hawaii_shp_info = m_hawaii.readshapefile('shapefiles/st99_d00', 'states',drawbounds=True)
return map_lower_48, m_alaska, m_hawaii
# make a colormap coloring each state according to its value in dictionary d
def make_statemap(d):
map_lower_48, m_alaska, m_hawaii = get_maps()
cmap = plt.cm.Blues
vmin = 0; vmax = 1
fig = plt.figure(figsize = (15, 24), dpi=300)
ax_mainland = plt.subplot2grid((3,2),(0,0), colspan=2, rowspan=2)
plt.text(0.5, 1.05, "Number of TRAP Laws", transform = ax_mainland.transAxes)
ax_mainland.axis("off")
#map for the lower 48
colors = {}
state_names = []
for shapedict in map_lower_48.states_info:
state = shapedict['NAME']
if state != 'Puerto Rico' and state != 'District of Columbia':
colors[state] = cmap(d[state]) #maps the colors to the states in a dictionary called "colors"
state_names.append(state)
for nshape, seg in enumerate(map_lower_48.states):
state = state_names[nshape]
if state != 'Puerto Rico' and state != 'District of Columbia':
color = rgb2hex(colors[state])
mainland_poly = Polygon(seg, facecolor=color, edgecolor=color)
ax_mainland.add_patch(mainland_poly)
map_lower_48.drawstates(color=color, linewidth=0)
#Repeat for Alaska
colors = {}
ax_alaska = plt.subplot2grid((3,2), (2,0), colspan = 1, rowspan=1)
#ax_alaska.set_title('Alaska')
ax_alaska.axis("off") #prevents border around state
for shapedict in m_alaska.states_info:
colors['Alaska'] = cmap(d['Alaska'])
state_names.append('Alaska')
for nshape, seg in enumerate(m_alaska.states):
color_alaska = rgb2hex(colors['Alaska'])
alaska_poly = Polygon(seg, facecolor=color_alaska, edgecolor=color_alaska)
ax_alaska.add_patch(alaska_poly)
m_alaska.drawstates(color=color, linewidth=0)
#Repeat for Hawaii
colors = {}
ax_hawaii = plt.subplot2grid((3,2), (2,1), colspan = 1, rowspan=1)
#ax_hawaii.set_title('Hawaii')
ax_hawaii.axis("off")
for shapedict in m_hawaii.states_info:
colors['Hawaii'] = cmap(d['Hawaii'])
state_names.append('Hawaii')
for nshape, seg in enumerate(m_hawaii.states):
color_hawaii = rgb2hex(colors['Hawaii'])
hawaii_poly = Polygon(seg, facecolor=color_hawaii, edgecolor=color_hawaii)
ax_hawaii.add_patch(hawaii_poly)
m_hawaii.drawstates(color=color_hawaii, linewidth=0)
plt.show()
#plt.savefig('state_name_scores.png', dpi=300) #bbox_inces='tight')
# make a colormap of states according to how many TRAP laws they have
def plot_state_traplaws():
d = {}
for s in lawsdata.state:
d[s] = int(lawsdata.test[lawsdata.state == s]*2)
make_statemap(d)
# plot a heatmap of driving time to nearest clinic for each state
def plot_drivetime_heatmap():
m = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49,
projection='lcc',lat_1=33,lat_2=45,lon_0=-95)
states_shp_info = m.readshapefile('shapefiles/st99_d00', 'states',drawbounds=True)
x,y = m(cities['lng'].values, cities['lat'].values)
vals = cities['totaltime'].values
vals = [v for v in vals] # values are hours
m.scatter(x,y, c=vals, cmap='jet', vmin=0, vmax=12, lw=0)
m.drawstates(linewidth=0)
plt.colorbar()
#plt.title("Number of Nights You'll Have to Stay in a Hotel if You Need an Abortion (based on hometown city)")
#plt.title("Total Time Spent away from Hometown when Getting Abortion (including driving time)")
#plt.title("Driving Time in Hours from City to Closest Clinic")
# Show the map
plt.show()
def make_ranks_vals(attr):
s = []
for x in cities[attr]:
if x > 0:
s.append(x)
s.sort()
print s
for i in range(len(s)):
plt.plot(i,s[i],'ro')
plt.show()
if __name__ == "__main__":
data = pandas.read_csv('ablocs.csv')
cities = pandas.read_csv('cities.csv')
lawsdata = pandas.read_csv('ablaws.csv')
# uncomment as needed
make_ranks_vals("totaltime")
#plot_drivetime_heatmap()
#plot_state_traplaws()
#plot_clinic_locations()
# cities['drivetime'].dropna()
# plt.hist(cities['drivetime'].dropna(), bins=range(int(min(cities.drivetime.dropna())), int(max(cities.drivetime.dropna())) + 15, 15))
# plt.axis([0,600,0,4500])
# plt.show()