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

Skip to content

Commit 5f25d20

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 parent 6241384 commit 5f25d20

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
import matplotlib.pyplot as plt
2727

28-
from matplotlib.transforms import Affine2D, offset_copy
29-
3028

3129
def rainbow_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+
can set the font size, family, etc.
5149
"""
5250
if ax is None:
5351
ax = plt.gca()
54-
t = ax.transData
55-
fig = ax.figure
56-
canvas = fig.canvas
5752

5853
assert orientation in ['horizontal', 'vertical']
59-
if orientation == 'vertical':
60-
kwargs.update(rotation=90, verticalalignment='bottom')
61-
62-
for s, c in zip(strings, colors):
63-
text = ax.text(x, y, s + " ", color=c, transform=t, **kwargs)
54+
if orientation == 'horizontal':
55+
txt = ax.text(x, y, strings[0], color=colors[0], **kwargs)
56+
for s, c in zip(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-
if orientation == '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+
elif orientation == 'vertical':
61+
kwargs.update(rotation=90, verticalalignment='bottom')
62+
txt = ax.text(x, y, strings[0], color=colors[0], **kwargs)
63+
for s, c in zip(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)