|
1 |
| -#!/usr/bin/env python |
2 |
| - |
| 1 | +""" |
| 2 | +Selected features of Matplotlib's math rendering engine. |
| 3 | +""" |
3 | 4 | from __future__importprint_function
|
| 5 | +importmatplotlib.pyplotasplt |
| 6 | +importos |
| 7 | +importsys |
| 8 | +importre |
| 9 | +importgc |
4 | 10 |
|
5 |
| -importos,sys,re |
| 11 | +# Selection of features following "Writing mathematical expressions" tutorial |
| 12 | +mathtext_titles= { |
| 13 | +0:"Header demo", |
| 14 | +1:"Subscripts and superscripts", |
| 15 | +2:"Fractions, binomials and stacked numbers", |
| 16 | +3:"Radicals", |
| 17 | +4:"Fonts", |
| 18 | +5:"Accents", |
| 19 | +6:"Greek, Hebrew", |
| 20 | +7:"Delimiters, functions and Symbols"} |
| 21 | +n_lines=len(mathtext_titles) |
6 | 22 |
|
7 |
| -importgc |
| 23 | +# Randomly picked examples |
| 24 | +mathext_demos= { |
| 25 | +0:r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = " |
| 26 | +r"U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} " |
| 27 | +r"\int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ " |
| 28 | +r"U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_" |
| 29 | +r"{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$", |
| 30 | + |
| 31 | +1:r"$\alpha_i > \beta_i,\ " |
| 32 | +r"\alpha_{i+1}^j = {\rm sin}(2\pi f_j t_i) e^{-5 t_i/\tau},\ " |
| 33 | +r"\ldots$", |
| 34 | + |
| 35 | +2:r"$\frac{3}{4},\ \binom{3}{4},\ \stackrel{3}{4},\ " |
| 36 | +r"\left(\frac{5 - \frac{1}{x}}{4}\right),\ \ldots$", |
8 | 37 |
|
9 |
| -stests= [ |
10 |
| -r'$\left[\left\lfloor\frac{5}{\frac{\left(3\right)}{4}} y\right)\right]$', |
11 |
| -r"$\gamma = \frac{x=\frac{6}{8}}{y} \delta$", |
12 |
| -r'$\limsup_{x\to\infty}$', |
13 |
| -r'$\oint^\infty_0$', |
14 |
| -r"$\sqrt[5]{\prod^\frac{x}{2\pi^2}_\infty}$", |
15 |
| -# From UTR #25 |
16 |
| -r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} \int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$", |
17 |
| -r'$\mathcal{H} = \int d \tau \left(\epsilon E^2 + \mu H^2\right)$', |
18 |
| -r'$\widehat{abc}\widetilde{def}$', |
19 |
| -#ur'Generic symbol: $\u23ce$', |
20 |
| - ] |
| 38 | +3:r"$\sqrt{2},\ \sqrt[3]{x},\ \ldots$", |
21 | 39 |
|
22 |
| -#if sys.maxunicode > 0xffff: |
23 |
| -#stests.append(ur'$\mathrm{\ue0f2 \U0001D538}$') |
| 40 | +4:r"$\mathrm{Roman}\ , \ \mathit{Italic}\ , \ \mathtt{Typewriter} \ " |
| 41 | +r"\mathrm{or}\ \mathcal{CALLIGRAPHY}$", |
24 | 42 |
|
| 43 | +5:r"$\acute a,\ \bar a,\ \breve a,\ \dot a,\ \ddot a, \ \grave a, \ " |
| 44 | +r"\hat a,\ \tilde a,\ \vec a,\ \widehat{xyz},\ \widetilde{xyz},\ " |
| 45 | +r"\ldots$", |
| 46 | + |
| 47 | +6: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 | +7:r"$\coprod,\ \int,\ \oint,\ \prod,\ \sum,\ " |
| 52 | +r"\log,\ \sin,\ \approx,\ \oplus,\ \star,\ \varpropto,\ " |
| 53 | +r"\infty,\ \partial,\ \Re,\ \leftrightsquigarrow, \ \ldots$"} |
25 | 54 |
|
26 |
| -frompylabimport* |
27 | 55 |
|
28 | 56 | defdoall():
|
29 |
| -tests=stests |
| 57 | +# Colors used in mpl online documentation. |
| 58 | +mpl_blue_rvb= (191./255.,209./256.,212./255.) |
| 59 | +mpl_orange_rvb= (202/255.,121/256.,0./255.) |
| 60 | +mpl_grey_rvb= (51./255.,51./255.,51./255.) |
| 61 | + |
| 62 | +# Creating figure and axis. |
| 63 | +plt.figure(figsize=(6,7)) |
| 64 | +plt.axes([0.01,0.01,0.98,0.90],axisbg="white",frameon=True) |
| 65 | +plt.gca().set_xlim(0.,1.) |
| 66 | +plt.gca().set_ylim(0.,1.) |
| 67 | +plt.gca().set_title("Matplotlib's math rendering engine", |
| 68 | +color=mpl_grey_rvb,fontsize=14,weight='bold') |
| 69 | +plt.gca().set_xticklabels("",visible=False) |
| 70 | +plt.gca().set_yticklabels("",visible=False) |
| 71 | + |
| 72 | +# Gap between lines in axes coords |
| 73 | +line_axesfrac= (1./ (n_lines)) |
| 74 | + |
| 75 | +# Plotting header demonstration formula |
| 76 | +full_demo=mathext_demos[0] |
| 77 | +plt.annotate(full_demo, |
| 78 | +xy=(0.5,1.-0.59*line_axesfrac), |
| 79 | +xycoords='data',color=mpl_orange_rvb,ha='center', |
| 80 | +fontsize=20) |
30 | 81 |
|
31 |
| -figure(figsize=(8, (len(tests)*1.0)+2),facecolor='w') |
32 |
| -fori,sinenumerate(tests): |
33 |
| -print (i,s) |
34 |
| -figtext(0.1,float(i+1)/ (len(tests)+2),s,fontsize=20) |
| 82 | +# Plotting features demonstration formulae |
| 83 | +fori_lineinrange(1,n_lines): |
| 84 | +baseline=1.- (i_line)*line_axesfrac |
| 85 | +baseline_next=baseline-line_axesfrac*1. |
| 86 | +title=mathtext_titles[i_line]+":" |
| 87 | +fill_color= ['white',mpl_blue_rvb][i_line%2] |
| 88 | +plt.fill_between([0.,1.], [baseline,baseline], |
| 89 | + [baseline_next,baseline_next], |
| 90 | +color=fill_color,alpha=0.5) |
| 91 | +plt.annotate(title, |
| 92 | +xy=(0.07,baseline-0.3*line_axesfrac), |
| 93 | +xycoords='data',color=mpl_grey_rvb,weight='bold') |
| 94 | +demo=mathext_demos[i_line] |
| 95 | +plt.annotate(demo, |
| 96 | +xy=(0.05,baseline-0.75*line_axesfrac), |
| 97 | +xycoords='data',color=mpl_grey_rvb, |
| 98 | +fontsize=16) |
35 | 99 |
|
36 |
| -savefig('mathtext_examples') |
37 |
| -#close('all') |
38 |
| -show() |
| 100 | +foriinrange(n_lines): |
| 101 | +s=mathext_demos[i] |
| 102 | +print(i,s) |
| 103 | +plt.show() |
39 | 104 |
|
40 | 105 | if'--latex'insys.argv:
|
| 106 | +# Run: python mathtext_examples.py --latex |
| 107 | +# Need amsmath and amssymb packages. |
41 | 108 | fd=open("mathtext_examples.ltx","w")
|
42 | 109 | fd.write("\\documentclass{article}\n")
|
| 110 | +fd.write("\\usepackage{amsmath, amssymb}\n") |
43 | 111 | fd.write("\\begin{document}\n")
|
44 | 112 | fd.write("\\begin{enumerate}\n")
|
45 | 113 |
|
46 |
| -fori,sinenumerate(stests): |
| 114 | +foriinrange(n_lines): |
| 115 | +s=mathext_demos[i] |
47 | 116 | s=re.sub(r"(?<!\\)\$","$$",s)
|
48 | 117 | fd.write("\\item %s\n"%s)
|
49 | 118 |
|
|