Usetex 演示

演示如何在绘制中使用latex。

另请参阅 “使用latex进行文本渲染” 指南。

Usetex 演示

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. plt.rc('text', usetex=True)
  4. # interface tracking profiles
  5. N = 500
  6. delta = 0.6
  7. X = np.linspace(-1, 1, N)
  8. plt.plot(X, (1 - np.tanh(4 * X / delta)) / 2, # phase field tanh profiles
  9. X, (1.4 + np.tanh(4 * X / delta)) / 4, "C2", # composition profile
  10. X, X < 0, 'k--') # sharp interface
  11. # legend
  12. plt.legend(('phase field', 'level set', 'sharp interface'),
  13. shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=16)
  14. # the arrow
  15. plt.annotate("", xy=(-delta / 2., 0.1), xycoords='data',
  16. xytext=(delta / 2., 0.1), textcoords='data',
  17. arrowprops=dict(arrowstyle="<->", connectionstyle="arc3"))
  18. plt.text(0, 0.1, r'$\delta$',
  19. {'color': 'k', 'fontsize': 24, 'ha': 'center', 'va': 'center',
  20. 'bbox': dict(boxstyle="round", fc="w", ec="k", pad=0.2)})
  21. # Use tex in labels
  22. plt.xticks((-1, 0, 1), ('$-1$', r'$\pm 0$', '$+1$'), color='k', size=20)
  23. # Left Y-axis labels, combine math mode and text mode
  24. plt.ylabel(r'\bf{phase field} $\phi$', {'color': 'C0', 'fontsize': 20})
  25. plt.yticks((0, 0.5, 1), (r'\bf{0}', r'\bf{.5}', r'\bf{1}'), color='k', size=20)
  26. # Right Y-axis labels
  27. plt.text(1.02, 0.5, r"\bf{level set} $\phi$", {'color': 'C2', 'fontsize': 20},
  28. horizontalalignment='left',
  29. verticalalignment='center',
  30. rotation=90,
  31. clip_on=False,
  32. transform=plt.gca().transAxes)
  33. # Use multiline environment inside a `text`.
  34. # level set equations
  35. eq1 = r"\begin{eqnarray*}" + \
  36. r"|\nabla\phi| &=& 1,\\" + \
  37. r"\frac{\partial \phi}{\partial t} + U|\nabla \phi| &=& 0 " + \
  38. r"\end{eqnarray*}"
  39. plt.text(1, 0.9, eq1, {'color': 'C2', 'fontsize': 18}, va="top", ha="right")
  40. # phase field equations
  41. eq2 = r'\begin{eqnarray*}' + \
  42. r'\mathcal{F} &=& \int f\left( \phi, c \right) dV, \\ ' + \
  43. r'\frac{ \partial \phi } { \partial t } &=& -M_{ \phi } ' + \
  44. r'\frac{ \delta \mathcal{F} } { \delta \phi }' + \
  45. r'\end{eqnarray*}'
  46. plt.text(0.18, 0.18, eq2, {'color': 'C0', 'fontsize': 16})
  47. plt.text(-1, .30, r'gamma: $\gamma$', {'color': 'r', 'fontsize': 20})
  48. plt.text(-1, .18, r'Omega: $\Omega$', {'color': 'b', 'fontsize': 20})
  49. plt.show()

下载这个示例