|
1 | 1 | """ |
2 | | -======================== |
3 | | -Math Fontfamily Example |
4 | | -======================== |
| 2 | +=============== |
| 3 | +Math fontfamily |
| 4 | +=============== |
5 | 5 |
|
6 | 6 | A simple example showcasing the new *math_fontfamily* parameter that can |
7 | 7 | be used to change the family of fonts for each individual text |
8 | | -element in a plot. If no parameter is set, the global value |
| 8 | +element in a plot. |
| 9 | +
|
| 10 | +If no parameter is set, the global value |
9 | 11 | :rc:`mathtext.fontset` will be used. |
10 | 12 | """ |
11 | 13 |
|
12 | 14 | import matplotlib.pyplot as plt |
13 | 15 |
|
14 | | -plt.figure(figsize=(9, 4)) |
| 16 | +plt.figure(figsize=(6, 5)) |
15 | 17 |
|
16 | 18 | # A simple plot for the background. |
17 | | -plt.plot(range(11), color="0.9", label=r'Math Font Example') |
18 | | - |
19 | | -# Here we hold the text, we will mix normal text and math text |
20 | | -msg = r"This is a normal text and " |
21 | | -msg+= r"$this\ is\ a\ text\ in\ math\ mode,\ look,\ I\ can\ do\ this\ " |
22 | | -msg+= r"\int_{0}^{\infty } x^2 dx !$" |
| 19 | +plt.plot(range(11), color="0.9") |
23 | 20 |
|
24 | | -# Now we set the text to be shown |
25 | | -plt.text(0, 6, msg, size=11, math_fontfamily='cm') |
| 21 | +# A text mixing normal text and math text. |
| 22 | +msg = (r"Normal Text. $Text\ in\ math\ mode:\ " |
| 23 | + r"\int_{0}^{\infty } x^2 dx$") |
26 | 24 |
|
27 | | -# The parameter math_fontfamily migth be aliased to math_font, the |
28 | | -# code below will work just as well. |
29 | | -plt.text(0, 3, msg, size=11, math_font='dejavuserif') |
| 25 | +# Set the text in the plot. |
| 26 | +plt.text(1, 7, msg, size=12, math_fontfamily='cm') |
30 | 27 |
|
31 | | -# Also, we can call *math_fontfamily* and *math_font* in most places where |
32 | | -# there is text, like in the title: |
33 | | -titleMsg = r"$This\ is\ a\ title\ in\ math\ mode,\ look:\ " |
34 | | -titleMsg+= r"\int_{0}^{\infty } x^2 dx !$" |
| 28 | +# Set another font for the next text. |
| 29 | +plt.text(1, 3, msg, size=12, math_fontfamily='dejavuserif') |
35 | 30 |
|
36 | | -plt.title(titleMsg, math_font='stixsans', size=14) |
| 31 | +# *math_fontfamily* can be used in most places where there is text, |
| 32 | +# like in the title: |
| 33 | +plt.title(r"$Title\ in\ math\ mode:\ \int_{0}^{\infty } x^2 dx$", |
| 34 | + math_fontfamily='stixsans', size=14) |
37 | 35 |
|
| 36 | +# Note that the normal text is not changed by *math_fontfamily*. |
38 | 37 | plt.show() |
0 commit comments