Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62a5ba4

Browse files
authored
Merge pull request #27365 from StefRe/fix/doc_menu
[DOC]: Fix menu example
2 parents 4f12525 + 6a7cd0e commit 62a5ba4

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

galleries/examples/widgets/menu.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
Menu
44
====
55
6+
Using texts to construct a simple menu.
67
"""
7-
88
import matplotlib.pyplot as plt
99

1010
import matplotlib.artist as artist
1111
import matplotlib.patches as patches
12-
from matplotlib.transforms import IdentityTransform
1312

1413

1514
class ItemProperties:
@@ -22,8 +21,8 @@ def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow',
2221

2322

2423
class MenuItem(artist.Artist):
25-
padx = 5
26-
pady = 5
24+
padx = 0.05 # inches
25+
pady = 0.05
2726

2827
def __init__(self, fig, labelstr, props=None, hoverprops=None,
2928
on_select=None):
@@ -41,14 +40,16 @@ def __init__(self, fig, labelstr, props=None, hoverprops=None,
4140

4241
self.on_select = on_select
4342

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,
4745
size=props.fontsize)
4846
self.text_bbox = self.label.get_window_extent(
4947
fig.canvas.get_renderer())
48+
self.text_bbox = fig.dpi_scale_trans.inverted().transform_bbox(self.text_bbox)
5049

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.
5253

5354
self.set_hover_props(False)
5455

@@ -63,7 +64,7 @@ def check_select(self, event):
6364

6465
def set_extent(self, x, y, w, h, depth):
6566
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))
6768
self.hover = False
6869

6970
def draw(self, renderer):
@@ -97,10 +98,10 @@ def __init__(self, fig, menuitems):
9798
maxh = max(item.text_bbox.height for item in menuitems)
9899
depth = max(-item.text_bbox.y0 for item in menuitems)
99100

100-
x0 = 100
101-
y0 = 400
101+
x0 = 1
102+
y0 = 4
102103

103-
width = maxw + 2*MenuItem.padx
104+
width = maxw + 2 * MenuItem.padx
104105
height = maxh + MenuItem.pady
105106

106107
for item in menuitems:

0 commit comments

Comments
 (0)