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

Skip to content

Commit 889abdf

Browse files
authored
Merge pull request #13961 from meeseeksmachine/auto-backport-of-pr-13914-on-v3.1.x
Backport PR #13914 on branch v3.1.x (Improve Rainbow text example)
2 parents 31780e1 + 2a2e345 commit 889abdf

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

examples/text_labels_and_annotations/rainbow_text.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Example:
1616
1717
plt.ylabel("Today is cloudy.")
18+
1819
How can I show "today" as red, "is" as green and "cloudy." as blue?
1920
2021
Thanks.
@@ -26,43 +27,55 @@
2627
from matplotlib import transforms
2728

2829

29-
def rainbow_text(x, y, strings, colors, ax=None, **kw):
30+
def rainbow_text(x, y, strings, colors, orientation='horizontal',
31+
ax=None, **kwargs):
3032
"""
31-
Take a list of ``strings`` and ``colors`` and place them next to each
33+
Take a list of *strings* and *colors* and place them next to each
3234
other, with text strings[i] being shown in colors[i].
3335
34-
This example shows how to do both vertical and horizontal text, and will
35-
pass all keyword arguments to plt.text, so you can set the font size,
36-
family, etc.
37-
38-
The text will get added to the ``ax`` axes, if provided, otherwise the
39-
currently active axes will be used.
36+
Parameters
37+
----------
38+
x, y : float
39+
Text position in data coordinates.
40+
strings : list of str
41+
The strings to draw.
42+
colors : list of color
43+
The colors to use.
44+
orientation : {'horizontal', 'vertical'}
45+
ax : Axes, optional
46+
The Axes to draw into. If None, the current axes will be used.
47+
**kwargs
48+
All other keyword arguments are passed to plt.text(), so you can
49+
set the font size, family, etc.
4050
"""
4151
if ax is None:
4252
ax = plt.gca()
4353
t = ax.transData
4454
canvas = ax.figure.canvas
4555

46-
# horizontal version
47-
for s, c in zip(strings, colors):
48-
text = ax.text(x, y, s + " ", color=c, transform=t, **kw)
49-
text.draw(canvas.get_renderer())
50-
ex = text.get_window_extent()
51-
t = transforms.offset_copy(
52-
text.get_transform(), x=ex.width, units='dots')
56+
assert orientation in ['horizontal', 'vertical']
57+
if orientation == 'vertical':
58+
kwargs.update(rotation=90, verticalalignment='bottom')
5359

54-
# vertical version
5560
for s, c in zip(strings, colors):
56-
text = ax.text(x, y, s + " ", color=c, transform=t,
57-
rotation=90, va='bottom', ha='center', **kw)
61+
text = ax.text(x, y, s + " ", color=c, transform=t, **kwargs)
62+
63+
# Need to draw to update the text position.
5864
text.draw(canvas.get_renderer())
5965
ex = text.get_window_extent()
60-
t = transforms.offset_copy(
61-
text.get_transform(), y=ex.height, units='dots')
62-
63-
64-
rainbow_text(0, 0, "all unicorns poop rainbows ! ! !".split(),
65-
['red', 'cyan', 'brown', 'green', 'blue', 'purple', 'black'],
66-
size=16)
66+
if orientation == 'horizontal':
67+
t = transforms.offset_copy(
68+
text.get_transform(), x=ex.width, units='dots')
69+
else:
70+
t = transforms.offset_copy(
71+
text.get_transform(), y=ex.height, units='dots')
72+
73+
74+
words = "all unicorns poop rainbows ! ! !".split()
75+
colors = ['red', 'orange', 'gold', 'lawngreen', 'lightseagreen', 'royalblue',
76+
'blueviolet']
77+
plt.figure(figsize=(6, 6))
78+
rainbow_text(0.1, 0.05, words, colors, size=18)
79+
rainbow_text(0.05, 0.1, words, colors, orientation='vertical', size=18)
6780

6881
plt.show()

0 commit comments

Comments
 (0)