-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatPlotLibMap11152016.py
127 lines (98 loc) · 3.97 KB
/
MatPlotLibMap11152016.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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.pyplot import figure, show
from numpy.random import rand
import matplotlib.image as mpimg
from matplotlib.cbook import get_sample_data
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
from matplotlib._png import read_png
def visualize():
fig = plt.figure()
ax = plt.axes()
points_with_annotation = []
themap = Basemap(projection='gall',
llcrnrlon = -180,
llcrnrlat = -90,
urcrnrlon = 180,
urcrnrlat = 90,
resolution = 'l',
area_thresh = 100000.0,
)
themap.drawcoastlines()
themap.drawcountries()
themap.fillcontinents(color = 'gainsboro')
themap.drawmapboundary(fill_color='steelblue')
#site 1
x1, y1 = themap(0,44)
point, = themap.plot(x1, y1, 'o', color='Red',markersize=10)
fn = get_sample_data("/Users/rogpaxton/Galvanize/SiteMetrics/plot44.0.png", asfileobj=False)
arr_lena = read_png(fn)
imagebox = OffsetImage(arr_lena, zoom=0.5)
xy1 = (0, 44)
annotation = AnnotationBbox(imagebox, xy1,
xybox=(50, 150),
xycoords='data',
boxcoords="offset points",
pad=0.5,
arrowprops=dict(arrowstyle="->",
#connectionstyle="angle,angleA=0,angleB=90,rad=3")
))
annotation.set_visible(False)
points_with_annotation.append([point, annotation])
x2, y2 = themap(90,44)
themap.plot(x2, y2,
'o', # marker shape
color='Red', # marker colour
markersize=6 # marker size
)
#site 2
x2, y2 = themap(90,44)
point, = themap.plot(x2, y2, 'o', color='Red',markersize=10)
fn = get_sample_data("/Users/rogpaxton/Galvanize/SiteMetrics/plot45.0.png", asfileobj=False)
arr_lena = read_png(fn)
imagebox = OffsetImage(arr_lena, zoom=0.5)
xy2 = (90, 44)
annotation = AnnotationBbox(imagebox, xy2,
xybox=(50, 150),
xycoords='data',
boxcoords="offset points",
pad=0.5,
#arrowprops=dict(arrowstyle="->",
#connectionstyle="angle,angleA=0,angleB=90,rad=3")
)
annotation.set_visible(False)
points_with_annotation.append([point, annotation])
#site 3
x3, y3 = themap(120,-34)
point, = themap.plot(x3, y3, 'o', color='Red',markersize=10)
fn = get_sample_data("/Users/rogpaxton/Galvanize/SiteMetrics/plot46.0.png", asfileobj=False)
arr_lena = read_png(fn)
imagebox = OffsetImage(arr_lena, zoom=0.5)
#image = mpimg.imread("plot44.0.png")
xy3 = (120, -34)
annotation = AnnotationBbox(imagebox, xy3,
xybox=(50, 150),
xycoords='data',
boxcoords="offset points",
pad=0.5,
arrowprops=dict(arrowstyle="->",
connectionstyle="angle,angleA=0,angleB=90,rad=3")
)
annotation.set_visible(False)
points_with_annotation.append([point, annotation])
#Mouseover
def on_move(event):
visibility_changed = False
for point, annotation in points_with_annotation:
ax.add_artist(annotation)
should_be_visible = (point.contains(event)[0] == True)
if should_be_visible != annotation.get_visible():
visibility_changed = True
annotation.set_visible(should_be_visible)
if visibility_changed:
plt.draw()
on_move_id = fig.canvas.mpl_connect('motion_notify_event', on_move)
plt.show()
if __name__ == '__main__':
visualize()