|
1 | 1 | """
|
2 |
| -================================= |
3 |
| -Rendering math equation using TeX |
4 |
| -================================= |
| 2 | +================================== |
| 3 | +Rendering math equations using TeX |
| 4 | +================================== |
5 | 5 |
|
6 | 6 | You can use TeX to render all of your matplotlib text if the rc
|
7 | 7 | parameter ``text.usetex`` is set. This works currently on the agg and ps
|
|
30 | 30 | ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16)
|
31 | 31 | ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty'
|
32 | 32 | r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r')
|
| 33 | + |
| 34 | +############################################################################# |
| 35 | +# A more complex example. |
| 36 | + |
| 37 | +fig, ax = plt.subplots() |
| 38 | +# interface tracking profiles |
| 39 | +N = 500 |
| 40 | +delta = 0.6 |
| 41 | +X = np.linspace(-1, 1, N) |
| 42 | +ax.plot(X, (1 - np.tanh(4 * X / delta)) / 2, # phase field tanh profiles |
| 43 | + X, (1.4 + np.tanh(4 * X / delta)) / 4, "C2", # composition profile |
| 44 | + X, X < 0, "k--") # sharp interface |
| 45 | + |
| 46 | +# legend |
| 47 | +ax.legend(("phase field", "level set", "sharp interface"), |
| 48 | + shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=16) |
| 49 | + |
| 50 | +# the arrow |
| 51 | +ax.annotate("", xy=(-delta / 2., 0.1), xytext=(delta / 2., 0.1), |
| 52 | + arrowprops=dict(arrowstyle="<->", connectionstyle="arc3")) |
| 53 | +ax.text(0, 0.1, r"$\delta$", |
| 54 | + color="black", fontsize=24, |
| 55 | + horizontalalignment="center", verticalalignment="center", |
| 56 | + bbox=dict(boxstyle="round", fc="white", ec="black", pad=0.2)) |
| 57 | + |
| 58 | +# Use tex in labels |
| 59 | +ax.set_xticks([-1, 0, 1]) |
| 60 | +ax.set_xticklabels(["$-1$", r"$\pm 0$", "$+1$"], color="k", size=20) |
| 61 | + |
| 62 | +# Left Y-axis labels, combine math mode and text mode |
| 63 | +ax.set_ylabel(r"\bf{phase field} $\phi$", color="C0", fontsize=20) |
| 64 | +ax.set_yticks([0, 0.5, 1]) |
| 65 | +ax.set_yticklabels([r"\bf{0}", r"\bf{.5}", r"\bf{1}"], color="k", size=20) |
| 66 | + |
| 67 | +# Right Y-axis labels |
| 68 | +ax.text(1.02, 0.5, r"\bf{level set} $\phi$", |
| 69 | + color="C2", fontsize=20, rotation=90, |
| 70 | + horizontalalignment="left", verticalalignment="center", |
| 71 | + clip_on=False, transform=ax.transAxes) |
| 72 | + |
| 73 | +# Use multiline environment inside a `text`. |
| 74 | +# level set equations |
| 75 | +eq1 = (r"\begin{eqnarray*}" |
| 76 | + r"|\nabla\phi| &=& 1,\\" |
| 77 | + r"\frac{\partial \phi}{\partial t} + U|\nabla \phi| &=& 0 " |
| 78 | + r"\end{eqnarray*}") |
| 79 | +ax.text(1, 0.9, eq1, color="C2", fontsize=18, |
| 80 | + horizontalalignment="right", verticalalignment="top") |
| 81 | + |
| 82 | +# phase field equations |
| 83 | +eq2 = (r"\begin{eqnarray*}" |
| 84 | + r"\mathcal{F} &=& \int f\left( \phi, c \right) dV, \\ " |
| 85 | + r"\frac{ \partial \phi } { \partial t } &=& -M_{ \phi } " |
| 86 | + r"\frac{ \delta \mathcal{F} } { \delta \phi }" |
| 87 | + r"\end{eqnarray*}") |
| 88 | +ax.text(0.18, 0.18, eq2, color="C0", fontsize=16) |
| 89 | + |
| 90 | +ax.text(-1, .30, r"gamma: $\gamma$", color="r", fontsize=20) |
| 91 | +ax.text(-1, .18, r"Omega: $\Omega$", color="b", fontsize=20) |
| 92 | + |
33 | 93 | plt.show()
|
0 commit comments