4
4
====
5
5
6
6
"""
7
- import numpy as np
7
+
8
8
import matplotlib .artist as artist
9
- import matplotlib .colors as colors
10
- import matplotlib .image as image
11
- import matplotlib .mathtext as mathtext
12
9
import matplotlib .patches as patches
13
10
import matplotlib .pyplot as plt
11
+ from matplotlib .transforms import IdentityTransform
14
12
15
13
16
14
class ItemProperties :
@@ -23,7 +21,6 @@ def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow',
23
21
24
22
25
23
class MenuItem (artist .Artist ):
26
- parser = mathtext .MathTextParser ("Bitmap" )
27
24
padx = 5
28
25
pady = 5
29
26
@@ -43,15 +40,12 @@ def __init__(self, fig, labelstr, props=None, hoverprops=None,
43
40
44
41
self .on_select = on_select
45
42
46
- x , self .depth = self .parser .to_mask (
47
- labelstr , fontsize = props .fontsize , dpi = fig .dpi )
48
-
49
- self .labelwidth = x .shape [1 ]
50
- self .labelheight = x .shape [0 ]
51
-
52
- mask = np .zeros ((* x .shape , 4 ))
53
- mask [:, :, - 1 ] = x / 255
54
- self .label = image .FigureImage (fig , origin = 'upper' , array = mask )
43
+ # Setting the transform to IdentityTransform() lets us specify
44
+ # coordinates directly in pixels.
45
+ self .label = fig .text (0 , 0 , labelstr , transform = IdentityTransform (),
46
+ size = props .fontsize )
47
+ self .text_bbox = self .label .get_window_extent (
48
+ fig .canvas .get_renderer ())
55
49
56
50
self .rect = patches .Rectangle ((0 , 0 ), 1 , 1 ) # Will be updated later.
57
51
@@ -66,10 +60,9 @@ def check_select(self, event):
66
60
if self .on_select is not None :
67
61
self .on_select (self )
68
62
69
- def set_extent (self , x , y , w , h ):
63
+ def set_extent (self , x , y , w , h , depth ):
70
64
self .rect .set (x = x , y = y , width = w , height = h )
71
- self .label .ox = x + self .padx
72
- self .label .oy = y - self .depth + self .pady / 2
65
+ self .label .set (position = (x + self .padx , y + depth + self .pady / 2 ))
73
66
self .hover = False
74
67
75
68
def draw (self , renderer ):
@@ -78,7 +71,7 @@ def draw(self, renderer):
78
71
79
72
def set_hover_props (self , b ):
80
73
props = self .hoverprops if b else self .props
81
- self .label .get_array ()[..., : 3 ] = colors . to_rgb ( props .labelcolor )
74
+ self .label .set ( color = props .labelcolor )
82
75
self .rect .set (facecolor = props .bgcolor , alpha = props .alpha )
83
76
84
77
def set_hover (self , event ):
@@ -99,8 +92,9 @@ def __init__(self, fig, menuitems):
99
92
100
93
self .menuitems = menuitems
101
94
102
- maxw = max (item .labelwidth for item in menuitems )
103
- maxh = max (item .labelheight for item in menuitems )
95
+ maxw = max (item .text_bbox .width for item in menuitems )
96
+ maxh = max (item .text_bbox .height for item in menuitems )
97
+ depth = max (- item .text_bbox .y0 for item in menuitems )
104
98
105
99
x0 = 100
106
100
y0 = 400
@@ -112,7 +106,7 @@ def __init__(self, fig, menuitems):
112
106
left = x0
113
107
bottom = y0 - maxh - MenuItem .pady
114
108
115
- item .set_extent (left , bottom , width , height )
109
+ item .set_extent (left , bottom , width , height , depth )
116
110
117
111
fig .artists .append (item )
118
112
y0 -= maxh + MenuItem .pady
0 commit comments