44====
55
66"""
7- import numpy as np
7+
88import matplotlib .artist as artist
9- import matplotlib .colors as colors
10- import matplotlib .image as image
11- import matplotlib .mathtext as mathtext
129import matplotlib .patches as patches
1310import matplotlib .pyplot as plt
11+ from matplotlib .transforms import IdentityTransform
1412
1513
1614class ItemProperties :
@@ -23,7 +21,6 @@ def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow',
2321
2422
2523class MenuItem (artist .Artist ):
26- parser = mathtext .MathTextParser ("Bitmap" )
2724 padx = 5
2825 pady = 5
2926
@@ -43,15 +40,12 @@ def __init__(self, fig, labelstr, props=None, hoverprops=None,
4340
4441 self .on_select = on_select
4542
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 ())
5549
5650 self .rect = patches .Rectangle ((0 , 0 ), 1 , 1 ) # Will be updated later.
5751
@@ -66,10 +60,9 @@ def check_select(self, event):
6660 if self .on_select is not None :
6761 self .on_select (self )
6862
69- def set_extent (self , x , y , w , h ):
63+ def set_extent (self , x , y , w , h , depth ):
7064 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 ))
7366 self .hover = False
7467
7568 def draw (self , renderer ):
@@ -78,7 +71,7 @@ def draw(self, renderer):
7871
7972 def set_hover_props (self , b ):
8073 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 )
8275 self .rect .set (facecolor = props .bgcolor , alpha = props .alpha )
8376
8477 def set_hover (self , event ):
@@ -99,8 +92,9 @@ def __init__(self, fig, menuitems):
9992
10093 self .menuitems = menuitems
10194
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 )
10498
10599 x0 = 100
106100 y0 = 400
@@ -112,7 +106,7 @@ def __init__(self, fig, menuitems):
112106 left = x0
113107 bottom = y0 - maxh - MenuItem .pady
114108
115- item .set_extent (left , bottom , width , height )
109+ item .set_extent (left , bottom , width , height , depth )
116110
117111 fig .artists .append (item )
118112 y0 -= maxh + MenuItem .pady
0 commit comments