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

Commit5f25d20

Browse files
Modify rainbow_text() function to use annotate() function (#25993)
* Modify rainbow_text() function to use annotate() function* Add doctring* Adjust code* Add doctring for function* Remove the variable t* Modify function doctring* Modify function doctring* Remove the excess space
1 parent6241384 commit5f25d20

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

‎galleries/examples/text_labels_and_annotations/rainbow_text.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
importmatplotlib.pyplotasplt
2727

28-
frommatplotlib.transformsimportAffine2D,offset_copy
29-
3028

3129
defrainbow_text(x,y,strings,colors,orientation='horizontal',
3230
ax=None,**kwargs):
@@ -45,42 +43,32 @@ def rainbow_text(x, y, strings, colors, orientation='horizontal',
4543
orientation : {'horizontal', 'vertical'}
4644
ax : Axes, optional
4745
The Axes to draw into. If None, the current axes will be used.
48-
**kwargs
49-
All other keyword arguments are passed to plt.text(), so you can
50-
set the font size, family, etc.
46+
**kwargs :
47+
All other keyword arguments are passed to plt.text() and plt.annotate(), so you
48+
canset the font size, family, etc.
5149
"""
5250
ifaxisNone:
5351
ax=plt.gca()
54-
t=ax.transData
55-
fig=ax.figure
56-
canvas=fig.canvas
5752

5853
assertorientationin ['horizontal','vertical']
59-
iforientation=='vertical':
60-
kwargs.update(rotation=90,verticalalignment='bottom')
61-
62-
fors,cinzip(strings,colors):
63-
text=ax.text(x,y,s+" ",color=c,transform=t,**kwargs)
54+
iforientation=='horizontal':
55+
txt=ax.text(x,y,strings[0],color=colors[0],**kwargs)
56+
fors,cinzip(strings[1:],colors[1:]):
57+
txt=ax.annotate(' '+s,xy=(1,0),xycoords=txt,
58+
va="bottom",color=c,**kwargs)
6459

65-
# Need to draw to update the text position.
66-
text.draw(canvas.get_renderer())
67-
ex=text.get_window_extent()
68-
# Convert window extent from pixels to inches
69-
# to avoid issues displaying at different dpi
70-
ex=fig.dpi_scale_trans.inverted().transform_bbox(ex)
71-
72-
iforientation=='horizontal':
73-
t=text.get_transform()+ \
74-
offset_copy(Affine2D(),fig=fig,x=ex.width,y=0)
75-
else:
76-
t=text.get_transform()+ \
77-
offset_copy(Affine2D(),fig=fig,x=0,y=ex.height)
60+
eliforientation=='vertical':
61+
kwargs.update(rotation=90,verticalalignment='bottom')
62+
txt=ax.text(x,y,strings[0],color=colors[0],**kwargs)
63+
fors,cinzip(strings[1:],colors[1:]):
64+
txt=ax.annotate(' '+s,xy=(0,1),xycoords=txt,
65+
va="bottom",color=c,**kwargs)
7866

7967

8068
words="all unicorns poop rainbows ! ! !".split()
8169
colors= ['red','orange','gold','lawngreen','lightseagreen','royalblue',
8270
'blueviolet']
83-
plt.figure(figsize=(6,6))
71+
plt.figure(figsize=(8,8))
8472
rainbow_text(0.1,0.05,words,colors,size=18)
8573
rainbow_text(0.05,0.1,words,colors,orientation='vertical',size=18)
8674

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp