|
5 | 5 |
|
6 | 6 | Selected features of Matplotlib's math rendering engine.
|
7 | 7 | """
|
8 |
| -import matplotlib.pyplot as plt |
| 8 | +import re |
9 | 9 | import subprocess
|
10 | 10 | import sys
|
11 |
| -import re |
12 | 11 |
|
13 |
| -# Selection of features following "Writing mathematical expressions" tutorial |
14 |
| -mathtext_titles = { |
15 |
| - 0: "Header demo", |
16 |
| - 1: "Subscripts and superscripts", |
17 |
| - 2: "Fractions, binomials and stacked numbers", |
18 |
| - 3: "Radicals", |
19 |
| - 4: "Fonts", |
20 |
| - 5: "Accents", |
21 |
| - 6: "Greek, Hebrew", |
22 |
| - 7: "Delimiters, functions and Symbols"} |
23 |
| -n_lines = len(mathtext_titles) |
| 12 | +import matplotlib.pyplot as plt |
24 | 13 |
|
25 |
| -# Randomly picked examples |
26 |
| -mathext_demos = { |
27 |
| - 0: r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = " |
28 |
| - r"U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} " |
29 |
| - r"\int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ " |
30 |
| - r"U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_" |
31 |
| - r"{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$", |
32 | 14 |
|
33 |
| - 1: r"$\alpha_i > \beta_i,\ " |
34 |
| - r"\alpha_{i+1}^j = {\rm sin}(2\pi f_j t_i) e^{-5 t_i/\tau},\ " |
35 |
| - r"\ldots$", |
| 15 | +# Selection of features following "Writing mathematical expressions" tutorial, |
| 16 | +# with randomly picked examples. |
| 17 | +mathtext_demos = { |
| 18 | + "Header demo": |
| 19 | + r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = " |
| 20 | + r"U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} " |
| 21 | + r"\int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ " |
| 22 | + r"U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_" |
| 23 | + r"{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$", |
36 | 24 |
|
37 |
| - 2: r"$\frac{3}{4},\ \binom{3}{4},\ \genfrac{}{}{0}{}{3}{4},\ " |
38 |
| - r"\left(\frac{5 - \frac{1}{x}}{4}\right),\ \ldots$", |
| 25 | + "Subscripts and superscripts": |
| 26 | + r"$\alpha_i > \beta_i,\ " |
| 27 | + r"\alpha_{i+1}^j = {\rm sin}(2\pi f_j t_i) e^{-5 t_i/\tau},\ " |
| 28 | + r"\ldots$", |
39 | 29 |
|
40 |
| - 3: r"$\sqrt{2},\ \sqrt[3]{x},\ \ldots$", |
| 30 | + "Fractions, binomials and stacked numbers": |
| 31 | + r"$\frac{3}{4},\ \binom{3}{4},\ \genfrac{}{}{0}{}{3}{4},\ " |
| 32 | + r"\left(\frac{5 - \frac{1}{x}}{4}\right),\ \ldots$", |
41 | 33 |
|
42 |
| - 4: r"$\mathrm{Roman}\ , \ \mathit{Italic}\ , \ \mathtt{Typewriter} \ " |
43 |
| - r"\mathrm{or}\ \mathcal{CALLIGRAPHY}$", |
| 34 | + "Radicals": |
| 35 | + r"$\sqrt{2},\ \sqrt[3]{x},\ \ldots$", |
44 | 36 |
|
45 |
| - 5: r"$\acute a,\ \bar a,\ \breve a,\ \dot a,\ \ddot a, \ \grave a, \ " |
46 |
| - r"\hat a,\ \tilde a,\ \vec a,\ \widehat{xyz},\ \widetilde{xyz},\ " |
47 |
| - r"\ldots$", |
| 37 | + "Fonts": |
| 38 | + r"$\mathrm{Roman}\ , \ \mathit{Italic}\ , \ \mathtt{Typewriter} \ " |
| 39 | + r"\mathrm{or}\ \mathcal{CALLIGRAPHY}$", |
48 | 40 |
|
49 |
| - 6: r"$\alpha,\ \beta,\ \chi,\ \delta,\ \lambda,\ \mu,\ " |
50 |
| - r"\Delta,\ \Gamma,\ \Omega,\ \Phi,\ \Pi,\ \Upsilon,\ \nabla,\ " |
51 |
| - r"\aleph,\ \beth,\ \daleth,\ \gimel,\ \ldots$", |
| 41 | + "Accents": |
| 42 | + r"$\acute a,\ \bar a,\ \breve a,\ \dot a,\ \ddot a, \ \grave a, \ " |
| 43 | + r"\hat a,\ \tilde a,\ \vec a,\ \widehat{xyz},\ \widetilde{xyz},\ " |
| 44 | + r"\ldots$", |
52 | 45 |
|
53 |
| - 7: r"$\coprod,\ \int,\ \oint,\ \prod,\ \sum,\ " |
54 |
| - r"\log,\ \sin,\ \approx,\ \oplus,\ \star,\ \varpropto,\ " |
55 |
| - r"\infty,\ \partial,\ \Re,\ \leftrightsquigarrow, \ \ldots$"} |
| 46 | + "Greek, Hebrew": |
| 47 | + r"$\alpha,\ \beta,\ \chi,\ \delta,\ \lambda,\ \mu,\ " |
| 48 | + r"\Delta,\ \Gamma,\ \Omega,\ \Phi,\ \Pi,\ \Upsilon,\ \nabla,\ " |
| 49 | + r"\aleph,\ \beth,\ \daleth,\ \gimel,\ \ldots$", |
| 50 | + |
| 51 | + "Delimiters, functions and Symbols": |
| 52 | + r"$\coprod,\ \int,\ \oint,\ \prod,\ \sum,\ " |
| 53 | + r"\log,\ \sin,\ \approx,\ \oplus,\ \star,\ \varpropto,\ " |
| 54 | + r"\infty,\ \partial,\ \Re,\ \leftrightsquigarrow, \ \ldots$", |
| 55 | +} |
| 56 | +n_lines = len(mathtext_demos) |
56 | 57 |
|
57 | 58 |
|
58 | 59 | def doall():
|
59 | 60 | # Colors used in Matplotlib online documentation.
|
60 |
| - mpl_blue_rvb = (191. / 255., 209. / 256., 212. / 255.) |
61 |
| - mpl_orange_rvb = (202. / 255., 121. / 256., 0. / 255.) |
62 |
| - mpl_grey_rvb = (51. / 255., 51. / 255., 51. / 255.) |
| 61 | + mpl_grey_rgb = (51 / 255, 51 / 255, 51 / 255) |
63 | 62 |
|
64 | 63 | # Creating figure and axis.
|
65 |
| - plt.figure(figsize=(6, 7)) |
66 |
| - plt.axes([0.01, 0.01, 0.98, 0.90], facecolor="white", frameon=True) |
67 |
| - plt.gca().set_xlim(0., 1.) |
68 |
| - plt.gca().set_ylim(0., 1.) |
69 |
| - plt.gca().set_title("Matplotlib's math rendering engine", |
70 |
| - color=mpl_grey_rvb, fontsize=14, weight='bold') |
71 |
| - plt.gca().set_xticklabels([]) |
72 |
| - plt.gca().set_yticklabels([]) |
| 64 | + fig = plt.figure(figsize=(7, 7)) |
| 65 | + ax = fig.add_axes([0.01, 0.01, 0.98, 0.90], |
| 66 | + facecolor="white", frameon=True) |
| 67 | + ax.set_xlim(0, 1) |
| 68 | + ax.set_ylim(0, 1) |
| 69 | + ax.set_title("Matplotlib's math rendering engine", |
| 70 | + color=mpl_grey_rgb, fontsize=14, weight='bold') |
| 71 | + ax.set_xticks([]) |
| 72 | + ax.set_yticks([]) |
73 | 73 |
|
74 | 74 | # Gap between lines in axes coords
|
75 | 75 | line_axesfrac = 1 / n_lines
|
76 | 76 |
|
77 |
| - # Plotting header demonstration formula |
78 |
| - full_demo = mathext_demos[0] |
79 |
| - plt.annotate(full_demo, |
80 |
| - xy=(0.5, 1. - 0.59 * line_axesfrac), |
81 |
| - color=mpl_orange_rvb, ha='center', fontsize=20) |
| 77 | + # Plot header demonstration formula. |
| 78 | + full_demo = mathtext_demos['Header demo'] |
| 79 | + ax.annotate(full_demo, |
| 80 | + xy=(0.5, 1. - 0.59 * line_axesfrac), |
| 81 | + color='tab:orange', ha='center', fontsize=20) |
| 82 | + |
| 83 | + # Plot feature demonstration formulae. |
| 84 | + for i_line, (title, demo) in enumerate(mathtext_demos.items()): |
| 85 | + print(i_line, demo) |
| 86 | + if i_line == 0: |
| 87 | + continue |
82 | 88 |
|
83 |
| - # Plotting features demonstration formulae |
84 |
| - for i_line in range(1, n_lines): |
85 | 89 | baseline = 1 - i_line * line_axesfrac
|
86 | 90 | baseline_next = baseline - line_axesfrac
|
87 |
| - title = mathtext_titles[i_line] + ":" |
88 |
| - fill_color = ['white', mpl_blue_rvb][i_line % 2] |
89 |
| - plt.fill_between([0., 1.], [baseline, baseline], |
90 |
| - [baseline_next, baseline_next], |
91 |
| - color=fill_color, alpha=0.5) |
92 |
| - plt.annotate(title, |
93 |
| - xy=(0.07, baseline - 0.3 * line_axesfrac), |
94 |
| - color=mpl_grey_rvb, weight='bold') |
95 |
| - demo = mathext_demos[i_line] |
96 |
| - plt.annotate(demo, |
97 |
| - xy=(0.05, baseline - 0.75 * line_axesfrac), |
98 |
| - color=mpl_grey_rvb, fontsize=16) |
99 |
| - |
100 |
| - for i in range(n_lines): |
101 |
| - s = mathext_demos[i] |
102 |
| - print(i, s) |
| 91 | + fill_color = ['white', 'tab:blue'][i_line % 2] |
| 92 | + ax.fill_between([0, 1], [baseline, baseline], |
| 93 | + [baseline_next, baseline_next], |
| 94 | + color=fill_color, alpha=0.2) |
| 95 | + ax.annotate(f'{title}:', |
| 96 | + xy=(0.06, baseline - 0.3 * line_axesfrac), |
| 97 | + color=mpl_grey_rgb, weight='bold') |
| 98 | + ax.annotate(demo, |
| 99 | + xy=(0.04, baseline - 0.75 * line_axesfrac), |
| 100 | + color=mpl_grey_rgb, fontsize=16) |
| 101 | + |
103 | 102 | plt.show()
|
104 | 103 |
|
105 | 104 |
|
106 | 105 | if '--latex' in sys.argv:
|
107 | 106 | # Run: python mathtext_examples.py --latex
|
108 | 107 | # Need amsmath and amssymb packages.
|
109 |
| - fd = open("mathtext_examples.ltx", "w") |
110 |
| - fd.write("\\documentclass{article}\n") |
111 |
| - fd.write("\\usepackage{amsmath, amssymb}\n") |
112 |
| - fd.write("\\begin{document}\n") |
113 |
| - fd.write("\\begin{enumerate}\n") |
114 |
| - |
115 |
| - for i in range(n_lines): |
116 |
| - s = mathext_demos[i] |
117 |
| - s = re.sub(r"(?<!\\)\$", "$$", s) |
118 |
| - fd.write("\\item %s\n" % s) |
119 |
| - |
120 |
| - fd.write("\\end{enumerate}\n") |
121 |
| - fd.write("\\end{document}\n") |
122 |
| - fd.close() |
| 108 | + with open("mathtext_examples.ltx", "w") as fd: |
| 109 | + fd.write("\\documentclass{article}\n") |
| 110 | + fd.write("\\usepackage{amsmath, amssymb}\n") |
| 111 | + fd.write("\\begin{document}\n") |
| 112 | + fd.write("\\begin{enumerate}\n") |
| 113 | + |
| 114 | + for s in mathtext_demos.values(): |
| 115 | + s = re.sub(r"(?<!\\)\$", "$$", s) |
| 116 | + fd.write("\\item %s\n" % s) |
| 117 | + |
| 118 | + fd.write("\\end{enumerate}\n") |
| 119 | + fd.write("\\end{document}\n") |
123 | 120 |
|
124 | 121 | subprocess.call(["pdflatex", "mathtext_examples.ltx"])
|
125 | 122 | else:
|
|
0 commit comments