|
15 | 15 | Example:
|
16 | 16 |
|
17 | 17 | plt.ylabel("Today is cloudy.")
|
| 18 | +
|
18 | 19 | How can I show "today" as red, "is" as green and "cloudy." as blue?
|
19 | 20 |
|
20 | 21 | Thanks.
|
|
26 | 27 | from matplotlib import transforms
|
27 | 28 |
|
28 | 29 |
|
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): |
30 | 32 | """
|
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 |
32 | 34 | other, with text strings[i] being shown in colors[i].
|
33 | 35 |
|
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. |
40 | 50 | """
|
41 | 51 | if ax is None:
|
42 | 52 | ax = plt.gca()
|
43 | 53 | t = ax.transData
|
44 | 54 | canvas = ax.figure.canvas
|
45 | 55 |
|
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') |
53 | 59 |
|
54 |
| - # vertical version |
55 | 60 | 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. |
58 | 64 | text.draw(canvas.get_renderer())
|
59 | 65 | 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) |
67 | 80 |
|
68 | 81 | plt.show()
|
0 commit comments