5
5
6
6
"""
7
7
import numpy as np
8
+ import matplotlib .artist as artist
8
9
import matplotlib .colors as colors
9
- import matplotlib .patches as patches
10
+ import matplotlib .image as image
10
11
import matplotlib .mathtext as mathtext
12
+ import matplotlib .patches as patches
11
13
import matplotlib .pyplot as plt
12
-import matplotlib .artist as artist
13
- import matplotlib .image as image
14
14
15
15
16
16
class ItemProperties :
@@ -21,9 +21,6 @@ def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow',
21
21
self .bgcolor = bgcolor
22
22
self .alpha = alpha
23
23
24
- self .labelcolor_rgb = colors .to_rgba (labelcolor )[:3 ]
25
- self .bgcolor_rgb = colors .to_rgba (bgcolor )[:3 ]
26
-
27
24
28
25
class MenuItem (artist .Artist ):
29
26
parser = mathtext .MathTextParser ("Bitmap" )
@@ -37,99 +34,70 @@ def __init__(self, fig, labelstr, props=None, hoverprops=None,
37
34
self .set_figure (fig )
38
35
self .labelstr = labelstr
39
36
40
- if props is None :
41
- props = ItemProperties ()
42
-
43
- if hoverprops is None :
44
- hoverprops = ItemProperties ()
45
-
46
- self .props = props
47
- self .hoverprops = hoverprops
37
+ self .props = props if props is not None else ItemProperties ()
38
+ self .hoverprops = (
39
+ hoverprops if hoverprops is not None else ItemProperties ())
40
+ if self .props .fontsize != self .hoverprops .fontsize :
41
+ raise NotImplementedError (
42
+ 'support for different font sizes not implemented' )
48
43
49
44
self .on_select = on_select
50
45
51
46
x , self .depth = self .parser .to_mask (
52
47
labelstr , fontsize = props .fontsize , dpi = fig .dpi )
53
48
54
- if props .fontsize != hoverprops .fontsize :
55
- raise NotImplementedError (
56
- 'support for different font sizes not implemented' )
57
-
58
49
self .labelwidth = x .shape [1 ]
59
50
self .labelheight = x .shape [0 ]
60
51
61
- self .labelArray = np .zeros ((x .shape [0 ], x .shape [1 ], 4 ))
62
- self .labelArray [:, :, - 1 ] = x / 255.
52
+ mask = np .zeros ((* x .shape , 4 ))
53
+ mask [:, :, - 1 ] = x / 255
54
+ self .label = image .FigureImage (fig , origin = 'upper' , array = mask )
63
55
64
- self .label = image .FigureImage (fig , origin = 'upper' )
65
- self .label .set_array (self .labelArray )
66
-
67
- # we'll update these later
68
- self .rect = patches .Rectangle ((0 , 0 ), 1 , 1 )
56
+ self .rect = patches .Rectangle ((0 , 0 ), 1 , 1 ) # Will be updated later.
69
57
70
58
self .set_hover_props (False )
71
59
72
60
fig .canvas .mpl_connect ('button_release_event' , self .check_select )
73
61
74
62
def check_select (self , event ):
75
- over , junk = self .rect .contains (event )
63
+ over , _ = self .rect .contains (event )
76
64
if not over :
77
65
return
78
-
79
66
if self .on_select is not None :
80
67
self .on_select (self )
81
68
82
69
def set_extent (self , x , y , w , h ):
83
- print (x , y , w , h )
84
- self .rect .set_x (x )
85
- self .rect .set_y (y )
86
- self .rect .set_width (w )
87
- self .rect .set_height (h )
88
-
70
+ self .rect .set (x = x , y = y , width = w , height = h )
89
71
self .label .ox = x + self .padx
90
- self .label .oy = y - self .depth + self .pady / 2.
91
-
72
+ self .label .oy = y - self .depth + self .pady / 2
92
73
self .hover = False
93
74
94
75
def draw (self , renderer ):
95
76
self .rect .draw (renderer )
96
77
self .label .draw (renderer )
97
78
98
79
def set_hover_props (self , b ):
99
- if b :
100
- props = self .hoverprops
101
- else :
102
- props = self .props
103
-
104
- r , g , b = props .labelcolor_rgb
105
- self .labelArray [:, :, 0 ] = r
106
- self .labelArray [:, :, 1 ] = g
107
- self .labelArray [:, :, 2 ] = b
108
- self .label .set_array (self .labelArray )
80
+ props = self .hoverprops if b else self .props
81
+ self .label .get_array ()[..., :3 ] = colors .to_rgb (props .labelcolor )
109
82
self .rect .set (facecolor = props .bgcolor , alpha = props .alpha )
110
83
111
84
def se
3419
t_hover (self , event ):
112
85
"""
113
86
Update the hover status of event and return whether it was changed.
114
87
"""
115
- b , junk = self .rect .contains (event )
116
-
88
+ b , _ = self .rect .contains (event )
117
89
changed = (b != self .hover )
118
-
119
90
if changed :
120
91
self .set_hover_props (b )
121
-
122
92
self .hover = b
123
93
return changed
124
94
125
95
126
96
class Menu :
127
97
def __init__ (self , fig , menuitems ):
128
98
self .figure = fig
129
- fig .suppressComposite = True
130
99
131
100
self .menuitems = menuitems
132
- self .numitems = len (menuitems )
133
101
134
102
maxw = max (item .labelwidth for item in menuitems )
135
103
maxh = max (item .labelheight for item in menuitems )
@@ -152,12 +120,8 @@ def __init__(self, fig, menuitems):
152
120
fig .canvas .mpl_connect ('motion_notify_event' , self .on_move )
153
121
154
122
def on_move (self , event ):
155
- draw = False
156
- for item in self .menuitems :
157
- draw = item .set_hover (event )
158
- if draw :
159
- self .figure .canvas .draw ()
160
- break
123
+ if any (item .set_hover (event ) for item in self .menuitems ):
124
+ self .figure .canvas .draw ()
161
125
162
126
163
127
fig = plt .figure ()
0 commit comments