数学文本例子

Matplotlib的数学渲染引擎的选定功能。

数学文本例子

Out:

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

下载这个示例