Skip to content

Commit b73f96b

Browse files
Doc: beautify usetex demo example
1 parent 928d6d5 commit b73f96b

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

examples/text_labels_and_annotations/usetex_demo.py

+37-44
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
Usetex Demo
44
===========
55
6+
Shows how to use latex in a plot.
7+
8+
Also refer to the :doc:`/tutorials/text/usetex` guide.
69
"""
10+
711
import matplotlib
812
matplotlib.rc('text', usetex=True)
913
import matplotlib.pyplot as plt
@@ -14,66 +18,55 @@
1418
delta = 0.6
1519
X = np.linspace(-1, 1, N)
1620
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
1922
X, X < 0, 'k--') # sharp interface
2023

2124
# 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)
3027

3128
# 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)})
4437

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)
4740

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)
5144

5245
# 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},
5447
horizontalalignment='left',
5548
verticalalignment='center',
5649
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)
6152

53+
# Use multiline environment inside a `text`.
6254
# 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")
6760

6861
# 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})
7468

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})
7871

7972
plt.show()

0 commit comments

Comments
 (0)