forked from KatieButler/Final-Project
/
matplotlib_test.py
182 lines (147 loc) · 5.52 KB
/
matplotlib_test.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
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.patches import Polygon
from matplotlib.pyplot import figure, show
fig_zoom = plt.figure()
fig = plt.figure()
# zoom_map = Basemap(projection='mill',
# llcrnrlat=40,
# llcrnrlon=-75,
# urcrnrlat=43,
# urcrnrlon=-69.5,
# resolution='c')
# zoom_map.drawcoastlines()
# zoom_map.drawcountries()
# zoom_map.drawmapboundary()
# zoom_map.drawstates()
my_map = Basemap(projection='merc',
lat_0=50,
lon_0=-100,
resolution = 'l',
area_thresh = 1000.0,
llcrnrlon=-125,
llcrnrlat=24,
urcrnrlon=-67,
urcrnrlat=50)
my_map.drawcoastlines()
my_map.drawcountries()
my_map.drawmapboundary()
my_map.drawstates()
plt.ion
my_map.readshapefile('st99_d00', name='states', drawbounds=True)
state_names = []
for shape_dict in my_map.states_info:
state_names.append(shape_dict['NAME'])
ax = plt.gca()
seg = my_map.states[state_names.index('Texas')]
poly = Polygon(seg, facecolor='red',edgecolor='red')
ax.add_patch(poly)
ax = plt.gca() # get current axes instance
def panda_file_to_list(file_name, title1, list1):
"""
The opens a saved csv file and convert it to a panda file. The
panda file is separated into columns and the columns are made into
lists. The lists are the output of the funciton.
"""
datafile = pd.read_csv(file_name) #opens a data file
# converts the data file into columns
for col in datafile.columns:
datafile[col] = datafile[col].astype(str)
column1 = datafile[title1]
# column2 = datafile[title2].astype(float)
# creates lists from the columns
list1 = []
for col_value in column1:
list2.append(col_value)
return list1
def remap_interval(val, input_start, input_end, output_start, output_end):
"""
takes an input interval, a value in the input interval and returns a
subsequent value in the output interval
>>> remap_interval(5, 0, 10, 0, 20)
10
"""
#everything needs to be a float
float(val)
float(input_end)
float(input_start)
float(output_end)
float(output_start)
#everything needs to be a float
input_space = float(input_end-input_start)
output_space = float(output_end-output_start)
diff = val - input_start
#gets the value in a form that compares it to the input interval
ratio = output_space/input_space
newval = output_start + diff*ratio
return int(newval)
def point_in_polygon(pt, polygon):
"""Returns True iff `pt` is inside `polygon`.
`polygon` is a list of tuples `(x, y)`."""
return matplotlib.path.Path(polygon).contains_point(pt)
def OnClick(event):
# print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
# event.button, event.x, event.y, event.xdata, event.ydata)
# for state in my_map.drawstates:
# mouse_in_state = any(point_in_polygon((event.x, event.y), state) for state in my_map.drawstates)
print "x=%d, y=%d"%(event.x, event.y)
def in_box(event):
if event.x >= 80 and event.y <= 200:
print "x=%d, y=%d"%(event.x, event.y)
else:
print "x=%d, y=%d w=%d"%(event.x, event.y, 0)
size = [350, 6694, 6309, 7401, 27728, 3787, 9339, 4588, 12840,
13589, 8529, 10971, 6668, 5590, 3779, 5565, 7019, 7829,
8001, 6234, 5477, 6548, 6266, 5710, 6851, 5391, 4289]
lats = [42.29, 42.37, 40.44,38.64,38.53,43.75,44.90,40.70,
43.13,42.40,41.48,39.74,42.83,
44.53,42.13,34.06,44.69,
37.42,33.79,46.55,36.65,41.31,41.82,43.13,
41.08,36.14,40.34,43.70]
lons = [-71.26, -71.11, -79.94,-90.31,-121.76,-71.69,-68.67,-89.61,
-70.93,-71.12,-71.53,-84.18,-88.74,
-87.92,-72.79,-117.16,-73.46,
-122.17,-84.32,-87.40,-121.81,-72.92,-71.40,-77.62,
-74.1768,-86.80,-74.74,-72.29]
def onpress(event):
if event.button != 1:
return
x, y = event.x, event.y
coord_lat = 40
coord_lon = -75
zoom_map = Basemap(projection='mill',
llcrnrlat=coord_lat,
llcrnrlon=coord_lon,
urcrnrlat=43,
urcrnrlon=-69.5,
resolution='c')
zoom_map.drawcoastlines()
zoom_map.drawcountries()
zoom_map.drawmapboundary()
zoom_map.drawstates()
for i in range(len(size)):
if size[i] <= 5000:
zoom_map.plot(x[i], y[i], 'go', markersize=size[i]/1000)
elif size[i] >= 10000:
zoom_map.plot(x[i], y[i], 'ro', markersize=size[i]/1000)
else:
zoom_map.plot(x[i], y[i], 'bo', markersize=size[i]/1000)
plt.show()
cid_up = fig.canvas.mpl_connect('button_press_event', in_box)
zoom1 = fig_zoom.canvas.mpl_connect('button_press_event', onpress)
#olin, harvard, cmu,washu,ucdavis,plymouth state,maine orno,bradley university
#unh,university of rhode island, u of dayton,u of wisconsin whitewater
#u of wisconsin green bay,westfield state,u of redlands,suny plattsburgh
#stanford,emory,northern michigan u,csu monterey bay,yale,brown,u of rochester,
#ramapo college,vanderbilt,princeton,dartmouth
x,y = my_map(lons, lats)
for i in range(len(size)):
if size[i] <= 5000:
my_map.plot(x[i], y[i], 'go', markersize=size[i]/1000)
elif size[i] >= 10000:
my_map.plot(x[i], y[i], 'ro', markersize=size[i]/1000)
else:
my_map.plot(x[i], y[i], 'bo', markersize=size[i]/1000)
plt.show()