Skip to content

Commit c41d423

Browse files
committed
Further cleanup rainbow_text example.
Share more code between horizontal and vertical cases. Show that x, y can use any transform (as for any text() call). Standardize docs of "extra" properties. Small doc rewordings.
1 parent 5f25d20 commit c41d423

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

galleries/examples/text_labels_and_annotations/rainbow_text.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,44 @@ def rainbow_text(x, y, strings, colors, orientation='horizontal',
3030
ax=None, **kwargs):
3131
"""
3232
Take a list of *strings* and *colors* and place them next to each
33-
other, with text strings[i] being shown in colors[i].
33+
other, with the nth text being shown in the nth color.
3434
3535
Parameters
3636
----------
3737
x, y : float
38-
Text position in data coordinates.
38+
The position of the first string. As for `~.Axes.text`, the coordinate
39+
system can be set using the *transform* parameter.
3940
strings : list of str
4041
The strings to draw.
4142
colors : list of color
4243
The colors to use.
4344
orientation : {'horizontal', 'vertical'}
44-
ax : Axes, optional
45-
The Axes to draw into. If None, the current axes will be used.
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.
45+
The orientation of the text.
46+
ax : Axes, default: the current Axes
47+
The Axes to draw into.
48+
**kwargs : `.Text` properties
4949
"""
5050
if ax is None:
5151
ax = plt.gca()
5252

53-
assert orientation in ['horizontal', 'vertical']
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)
53+
assert orientation in ["horizontal", "vertical"]
54+
if orientation == "horizontal":
55+
xy = (1, 0)
56+
elif orientation == "vertical":
57+
xy = (0, 1)
58+
kwargs.update(rotation=90)
5959

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)
60+
txt = ax.text(x, y, strings[0], color=colors[0], **kwargs)
61+
for s, c in zip(strings[1:], colors[1:]):
62+
txt = ax.annotate(" " + s, xy=xy, xycoords=txt,
63+
va="bottom", color=c, **kwargs)
6664

6765

6866
words = "all unicorns poop rainbows ! ! !".split()
6967
colors = ['red', 'orange', 'gold', 'lawngreen', 'lightseagreen', 'royalblue',
7068
'blueviolet']
71-
plt.figure(figsize=(8, 8))
72-
rainbow_text(0.1, 0.05, words, colors, size=18)
73-
rainbow_text(0.05, 0.1, words, colors, orientation='vertical', size=18)
69+
fig, ax = plt.subplots()
70+
rainbow_text(0.02, 0.02, words, colors, size=18, transform=fig.transFigure)
71+
rainbow_text(0.05, 0.1, words, colors, orientation='vertical')
7472

7573
plt.show()

0 commit comments

Comments
 (0)