|
3 | 3 | Rainbow text
|
4 | 4 | ============
|
5 | 5 |
|
6 |
| -The example shows how to string together several text objects. |
7 |
| -
|
8 |
| -History |
9 |
| -------- |
10 |
| -On the matplotlib-users list back in February 2012, Gökhan Sever asked the |
11 |
| -following question: |
12 |
| -
|
13 |
| - | Is there a way in matplotlib to partially specify the color of a string? |
14 |
| - | |
15 |
| - | Example: |
16 |
| - | |
17 |
| - | plt.ylabel("Today is cloudy.") |
18 |
| - | |
19 |
| - | How can I show "today" as red, "is" as green and "cloudy." as blue? |
20 |
| - | |
21 |
| - | Thanks. |
22 |
| -
|
23 |
| -The solution below is modified from Paul Ivanov's original answer. |
| 6 | +The example strings together several Text objects with different properties |
| 7 | +(e.g., color or font), positioning each one after the other: each Text other |
| 8 | +than the first one created with `~.Axes.annotate` and positioned so that its |
| 9 | +lower left corner is at the lower right corner (``xy=(1, 0)``) of the previous |
| 10 | +one (``xycoords=text``). |
24 | 11 | """
|
25 | 12 |
|
26 | 13 | import matplotlib.pyplot as plt
|
27 | 14 |
|
28 | 15 |
|
29 |
| -def rainbow_text(x, y, strings, colors, orientation='horizontal', |
30 |
| - ax=None, **kwargs): |
31 |
| - """ |
32 |
| - Take a list of *strings* and *colors* and place them next to each |
33 |
| - other, with text strings[i] being shown in colors[i]. |
34 |
| -
|
35 |
| - Parameters |
36 |
| - ---------- |
37 |
| - x, y : float |
38 |
| - Text position in data coordinates. |
39 |
| - strings : list of str |
40 |
| - The strings to draw. |
41 |
| - colors : list of color |
42 |
| - The colors to use. |
43 |
| - 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. |
49 |
| - """ |
50 |
| - if ax is None: |
51 |
| - ax = plt.gca() |
52 |
| - |
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) |
59 |
| - |
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) |
66 |
| - |
67 |
| - |
68 |
| -words = "all unicorns poop rainbows ! ! !".split() |
69 |
| -colors = ['red', 'orange', 'gold', 'lawngreen', 'lightseagreen', 'royalblue', |
70 |
| - '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) |
| 16 | +fig, ax = plt.subplots() |
| 17 | + |
| 18 | +# The first word, created with text(). |
| 19 | +text = ax.text(.1, .5, "Matplotlib", c="red") |
| 20 | +# Subsequent words, positioned with annotate(), relative to the preceding one. |
| 21 | +text = ax.annotate( |
| 22 | + " says,", xycoords=text, xy=(1, 0), verticalalignment="bottom", |
| 23 | + c="gold", weight="bold", # custom properties |
| 24 | +) |
| 25 | +text = ax.annotate( |
| 26 | + " hello", xycoords=text, xy=(1, 0), verticalalignment="bottom", |
| 27 | + c="green", style="italic", # custom properties |
| 28 | +) |
| 29 | +text = ax.annotate( |
| 30 | + " world!", xycoords=text, xy=(1, 0), verticalalignment="bottom", |
| 31 | + c="blue", family="serif", # custom properties |
| 32 | +) |
74 | 33 |
|
75 | 34 | plt.show()
|
0 commit comments