|
1 | 1 | """ |
2 | | -============ |
3 | | -Rainbow text |
4 | | -============ |
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. |
| 2 | +==================================================== |
| 3 | +Concatenating text objects with different properties |
| 4 | +==================================================== |
| 5 | +
|
| 6 | +The example strings together several Text objects with different properties |
| 7 | +(e.g., color or font), positioning each one after the other. The first Text |
| 8 | +is created directly using `~.Axes.text`; all subsequent ones are created with |
| 9 | +`~.Axes.annotate`, which allows positioning the Text's lower left corner at the |
| 10 | +lower right corner (``xy=(1, 0)``) of the previous one (``xycoords=text``). |
24 | 11 | """ |
25 | 12 |
|
26 | 13 | import matplotlib.pyplot as plt |
27 | 14 |
|
28 | | - |
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) |
| 15 | +plt.rcParams["font.size"] = 20 |
| 16 | +ax = plt.figure().add_subplot(xticks=[], yticks=[]) |
| 17 | + |
| 18 | +# The first word, created with text(). |
| 19 | +text = ax.text(.1, .5, "Matplotlib", color="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 | + color="gold", weight="bold") # custom properties |
| 24 | +text = ax.annotate( |
| 25 | + " hello", xycoords=text, xy=(1, 0), verticalalignment="bottom", |
| 26 | + color="green", style="italic") # custom properties |
| 27 | +text = ax.annotate( |
| 28 | + " world!", xycoords=text, xy=(1, 0), verticalalignment="bottom", |
| 29 | + color="blue", family="serif") # custom properties |
74 | 30 |
|
75 | 31 | plt.show() |
0 commit comments