|
3 | 3 | Usetex Demo
|
4 | 4 | ===========
|
5 | 5 |
|
| 6 | +Shows how to use latex in a plot. |
| 7 | +
|
| 8 | +Also refer to the :doc:`/tutorials/text/usetex` guide. |
6 | 9 | """
|
| 10 | + |
7 | 11 | import matplotlib
|
8 | 12 | matplotlib.rc('text', usetex=True)
|
9 | 13 | import matplotlib.pyplot as plt
|
|
14 | 18 | delta = 0.6
|
15 | 19 | X = np.linspace(-1, 1, N)
|
16 | 20 | plt.plot(X, (1 - np.tanh(4 * X / delta)) / 2, # phase field tanh profiles
|
17 |
| - X, (X + 1) / 2, # level set distance function |
18 |
| - X, (1.4 + np.tanh(4 * X / delta)) / 4, # composition profile |
| 21 | + X, (1.4 + np.tanh(4 * X / delta)) / 4, "C2", # composition profile |
19 | 22 | X, X < 0, 'k--') # sharp interface
|
20 | 23 |
|
21 | 24 | # legend
|
22 |
| -plt.legend(('phase field', 'level set', 'composition', 'sharp interface'), |
23 |
| - shadow=True, loc=(0.01, 0.55)) |
24 |
| - |
25 |
| -ltext = plt.gca().get_legend().get_texts() |
26 |
| -plt.setp(ltext[0], fontsize=20) |
27 |
| -plt.setp(ltext[1], fontsize=20) |
28 |
| -plt.setp(ltext[2], fontsize=20) |
29 |
| -plt.setp(ltext[3], fontsize=20) |
| 25 | +plt.legend(('phase field', 'level set', 'sharp interface'), |
| 26 | + shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=16) |
30 | 27 |
|
31 | 28 | # the arrow
|
32 |
| -height = 0.1 |
33 |
| -offset = 0.02 |
34 |
| -plt.plot((-delta / 2., delta / 2), (height, height), 'k', linewidth=2) |
35 |
| -plt.plot((-delta / 2, -delta / 2 + offset * 2), (height, height - offset), |
36 |
| - 'k', linewidth=2) |
37 |
| -plt.plot((-delta / 2, -delta / 2 + offset * 2), (height, height + offset), |
38 |
| - 'k', linewidth=2) |
39 |
| -plt.plot((delta / 2, delta / 2 - offset * 2), (height, height - offset), |
40 |
| - 'k', linewidth=2) |
41 |
| -plt.plot((delta / 2, delta / 2 - offset * 2), (height, height + offset), |
42 |
| - 'k', linewidth=2) |
43 |
| -plt.text(-0.06, height - 0.06, r'$\delta$', {'color': 'k', 'fontsize': 24}) |
| 29 | +plt.annotate("", |
| 30 | + xy=(-delta / 2., 0.1), xycoords='data', |
| 31 | + xytext=(delta / 2., 0.1), textcoords='data', |
| 32 | + arrowprops=dict(arrowstyle="<->", |
| 33 | + connectionstyle="arc3")) |
| 34 | +plt.text(0, 0.1, r'$\delta$', |
| 35 | + {'color': 'k', 'fontsize': 24, 'ha' : 'center', 'va' : 'center', |
| 36 | + 'bbox' : dict(boxstyle="round", fc="w", ec="k", pad=0.2)}) |
44 | 37 |
|
45 |
| -# X-axis label |
46 |
| -plt.xticks((-1, 0, 1), ('-1', '0', '1'), color='k', size=20) |
| 38 | +# Use tex in labels |
| 39 | +plt.xticks((-1, 0, 1), ('$-1$', r'$\pm 0$', '$+1$'), color='k', size=20) |
47 | 40 |
|
48 |
| -# Left Y-axis labels |
49 |
| -plt.ylabel(r'\bf{phase field} $\phi$', {'color': 'b', 'fontsize': 20}) |
50 |
| -plt.yticks((0, 0.5, 1), ('0', '.5', '1'), color='k', size=20) |
| 41 | +# Left Y-axis labels, combine math mode and text mode |
| 42 | +plt.ylabel(r'\bf{phase field} $\phi$', {'color': 'C0', 'fontsize': 20}) |
| 43 | +plt.yticks((0, 0.5, 1), (r'\bf{0}', r'\bf{.5}', r'\bf{1}'), color='k', size=20) |
51 | 44 |
|
52 | 45 | # Right Y-axis labels
|
53 |
| -plt.text(1.05, 0.5, r"\bf{level set} $\phi$", {'color': 'g', 'fontsize': 20}, |
| 46 | +plt.text(1.02, 0.5, r"\bf{level set} $\phi$", {'color': 'C2', 'fontsize': 20}, |
54 | 47 | horizontalalignment='left',
|
55 | 48 | verticalalignment='center',
|
56 | 49 | rotation=90,
|
57 |
| - clip_on=False) |
58 |
| -plt.text(1.01, -0.02, "-1", {'color': 'k', 'fontsize': 20}) |
59 |
| -plt.text(1.01, 0.98, "1", {'color': 'k', 'fontsize': 20}) |
60 |
| -plt.text(1.01, 0.48, "0", {'color': 'k', 'fontsize': 20}) |
| 50 | + clip_on=False, |
| 51 | + transform=plt.gca().transAxes) |
61 | 52 |
|
| 53 | +# Use multiline environment inside a `text`. |
62 | 54 | # level set equations
|
63 |
| -plt.text(0.1, 0.85, |
64 |
| - r'$|\nabla\phi| = 1,$ \newline $ \frac{\partial \phi}{\partial t}' |
65 |
| - r'+ U|\nabla \phi| = 0$', |
66 |
| - {'color': 'g', 'fontsize': 20}) |
| 55 | +eq1 = r"\begin{eqnarray*}" + \ |
| 56 | + r"|\nabla\phi| &=& 1,\\" + \ |
| 57 | + r"\frac{\partial \phi}{\partial t} + U|\nabla \phi| &=& 0 " + \ |
| 58 | + r"\end{eqnarray*}" |
| 59 | +plt.text(1, 0.9, eq1, {'color': 'C2', 'fontsize': 18}, va="top", ha="right") |
67 | 60 |
|
68 | 61 | # phase field equations
|
69 |
| -plt.text(0.2, 0.15, |
70 |
| - r'$\mathcal{F} = \int f\left( \phi, c \right) dV,$ \newline ' |
71 |
| - r'$ \frac{ \partial \phi } { \partial t } = -M_{ \phi } ' |
72 |
| - r'\frac{ \delta \mathcal{F} } { \delta \phi }$', |
73 |
| - {'color': 'b', 'fontsize': 20}) |
| 62 | +eq2 = r'\begin{eqnarray*}' + \ |
| 63 | + r'\mathcal{F} &=& \int f\left( \phi, c \right) dV, \\ ' + \ |
| 64 | + r'\frac{ \partial \phi } { \partial t } &=& -M_{ \phi } ' + \ |
| 65 | + r'\frac{ \delta \mathcal{F} } { \delta \phi }' + \ |
| 66 | + r'\end{eqnarray*}' |
| 67 | +plt.text(0.18, 0.18, eq2, {'color': 'C0', 'fontsize': 16}) |
74 | 68 |
|
75 |
| -# these went wrong in pdf in a previous version |
76 |
| -plt.text(-.9, .42, r'gamma: $\gamma$', {'color': 'r', 'fontsize': 20}) |
77 |
| -plt.text(-.9, .36, r'Omega: $\Omega$', {'color': 'b', 'fontsize': 20}) |
| 69 | +plt.text(-1, .30, r'gamma: $\gamma$', {'color': 'r', 'fontsize': 20}) |
| 70 | +plt.text(-1, .22, r'Omega: $\Omega$', {'color': 'b', 'fontsize': 20}) |
78 | 71 |
|
79 | 72 | plt.show()
|
0 commit comments