3
3
Menu
4
4
====
5
5
6
+ Using texts to construct a simple menu.
6
7
"""
7
-
8
8
import matplotlib .pyplot as plt
9
9
10
10
import matplotlib .artist as artist
11
11
import matplotlib .patches as patches
12
- from matplotlib .transforms import IdentityTransform
13
12
14
13
15
14
class ItemProperties :
@@ -22,8 +21,8 @@ def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow',
22
21
23
22
24
23
class MenuItem (artist .Artist ):
25
- padx = 5
26
- pady = 5
24
+ padx = 0.05 # inches
25
+ pady = 0.05
27
26
28
27
def __init__ (self , fig , labelstr , props = None , hoverprops = None ,
29
28
on_select = None ):
@@ -41,14 +40,16 @@ def __init__(self, fig, labelstr, props=None, hoverprops=None,
41
40
42
41
self .on_select = on_select
43
42
44
- # Setting the transform to IdentityTransform() lets us specify
45
- # coordinates directly in pixels.
46
- self .label = fig .text (0 , 0 , labelstr , transform = IdentityTransform (),
43
+ # specify coordinates in inches.
44
+ self .label = fig .text (0 , 0 , labelstr , transform = fig .dpi_scale_trans ,
47
45
size = props .fontsize )
48
46
self .text_bbox = self .label .get_window_extent (
49
47
fig .canvas .get_renderer ())
48
+ self .text_bbox = fig .dpi_scale_trans .inverted ().transform_bbox (self .text_bbox )
50
49
51
- self .rect = patches .Rectangle ((0 , 0 ), 1 , 1 ) # Will be updated later.
50
+ self .rect = patches .Rectangle (
51
+ (0 , 0 ), 1 , 1 , transform = fig .dpi_scale_trans
52
+ ) # Will be updated later.
52
53
53
54
self .set_hover_props (False )
54
55
@@ -63,7 +64,7 @@ def check_select(self, event):
63
64
64
65
def set_extent (self , x , y , w , h , depth ):
65
66
self .rect .set (x = x , y = y , width = w , height = h )
66
- self .label .set (position = (x + self .padx , y + depth + self .pady / 2 ))
67
+ self .label .set (position = (x + self .padx , y + depth + self .pady / 2 ))
67
68
self .hover = False
68
69
69
70
def draw (self , renderer ):
@@ -97,10 +98,10 @@ def __init__(self, fig, menuitems):
97
98
maxh = max (item .text_bbox .height for item in menuitems )
98
99
depth = max (- item .text_bbox .y0 for item in menuitems )
99
100
100
- x0 = 100
101
- y0 = 400
101
+ x0 = 1
102
+ y0 = 4
102
103
103
- width = maxw + 2 * MenuItem .padx
104
+ width = maxw + 2 * MenuItem .padx
104
105
height = maxh + MenuItem .pady
105
106
106
107
for item in menuitems :
0 commit comments