Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6a7cd0e

Browse files
committed
[DOC]: Fix menu example
Make example independent on dpi setting when saving the outputwith different resolutions to prevent shifting of texts and rects.
1 parent0a4efbe commit6a7cd0e

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
importmatplotlib.pyplotasplt
99

1010
importmatplotlib.artistasartist
1111
importmatplotlib.patchesaspatches
12-
frommatplotlib.transformsimportIdentityTransform
1312

1413

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

2322

2423
classMenuItem(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
defset_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
defdraw(self,renderer):
@@ -97,10 +98,10 @@ def __init__(self, fig, menuitems):
9798
maxh=max(item.text_bbox.heightforiteminmenuitems)
9899
depth=max(-item.text_bbox.y0foriteminmenuitems)
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
foriteminmenuitems:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp