Skip to content

Commit 5d6e96d

Browse files
committed
Merge pull request matplotlib#2066 from GBillotey/oversized_doc_img
[DOC] Mathtext and matshow examples
2 parents 3c9a12a + ff86f69 commit 5d6e96d

File tree

2 files changed

+104
-35
lines changed

2 files changed

+104
-35
lines changed

examples/pylab_examples/mathtext_examples.py

Lines changed: 97 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,118 @@
1-
#!/usr/bin/env python
2-
1+
"""
2+
Selected features of Matplotlib's math rendering engine.
3+
"""
34
from __future__ import print_function
5+
import matplotlib.pyplot as plt
6+
import os
7+
import sys
8+
import re
9+
import gc
410

5-
import os, 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)
622

7-
import gc
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$",
837

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$",
2139

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}$",
2442

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$"}
2554

26-
from pylab import *
2755

2856
def doall():
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)
3081

31-
figure(figsize=(8, (len(tests) * 1.0) + 2), facecolor='w')
32-
for i, s in enumerate(tests):
33-
print (i, s)
34-
figtext(0.1, float(i + 1) / (len(tests) + 2), s, fontsize=20)
82+
# Plotting features demonstration formulae
83+
for i_line in range(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)
3599

36-
savefig('mathtext_examples')
37-
#close('all')
38-
show()
100+
for i in range(n_lines):
101+
s = mathext_demos[i]
102+
print(i, s)
103+
plt.show()
39104

40105
if '--latex' in sys.argv:
106+
# Run: python mathtext_examples.py --latex
107+
# Need amsmath and amssymb packages.
41108
fd = open("mathtext_examples.ltx", "w")
42109
fd.write("\\documentclass{article}\n")
110+
fd.write("\\usepackage{amsmath, amssymb}\n")
43111
fd.write("\\begin{document}\n")
44112
fd.write("\\begin{enumerate}\n")
45113

46-
for i, s in enumerate(stests):
114+
for i in range(n_lines):
115+
s = mathext_demos[i]
47116
s = re.sub(r"(?<!\\)\$", "$$", s)
48117
fd.write("\\item %s\n" % s)
49118

examples/pylab_examples/matshow.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
#!/usr/bin/env python
21
"""Simple matshow() example."""
3-
42
from matplotlib.pylab import *
53

4+
65
def samplemat(dims):
76
"""Make a matrix with all zeros and increasing elements on the diagonal"""
87
aa = zeros(dims)
98
for i in range(min(dims)):
10-
aa[i,i] = i
9+
aa[i, i] = i
1110
return aa
1211

13-
# Make a few matrices of strange sizes
14-
dimlist = [(12,12),(128,64),(64,512),(1024,128)]
1512

13+
# Display 2 matrices of different sizes
14+
dimlist = [(12, 12), (15, 35)]
1615
for d in dimlist:
1716
matshow(samplemat(d))
1817

19-
# Display a random matrix with a specified figure number and a grayscale colormap
20-
matshow(rand(64,64),fignum=100,cmap=cm.gray)
18+
# Display a random matrix with a specified figure number and a grayscale
19+
# colormap
20+
matshow(rand(64, 64), fignum=100, cmap=cm.gray)
2121

2222
show()

0 commit comments

Comments
 (0)