Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30788aa

Browse files
timhoffmMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #11462: Doc: beautify usetex demo example
1 parent f67974c commit 30788aa

File tree

1 file changed

+37
-47
lines changed

1 file changed

+37
-47
lines changed

examples/text_labels_and_annotations/usetex_demo.py

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,67 @@
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
"""
7-
import matplotlib
8-
matplotlib.rc('text', usetex=True)
9-
import matplotlib.pyplot as plt
10+
1011
import numpy as np
12+
import matplotlib.pyplot as plt
13+
plt.rc('text', usetex=True)
1114

1215
# interface tracking profiles
1316
N = 500
1417
delta = 0.6
1518
X = np.linspace(-1, 1, N)
1619
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
20+
X, (1.4 + np.tanh(4 * X / delta)) / 4, "C2", # composition profile
1921
X, X < 0, 'k--') # sharp interface
2022

2123
# 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)
24+
plt.legend(('phase field', 'level set', 'sharp interface'),
25+
shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=16)
3026

3127
# 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})
28+
plt.annotate("", xy=(-delta / 2., 0.1), xycoords='data',
29+
xytext=(delta / 2., 0.1), textcoords='data',
30+
arrowprops=dict(arrowstyle="<->", connectionstyle="arc3"))
31+
plt.text(0, 0.1, r'$\delta$',
32+
{'color': 'k', 'fontsize': 24, 'ha' : 'center', 'va' : 'center',
33+
'bbox' : dict(boxstyle="round", fc="w", ec="k", pad=0.2)})
4434

45-
# X-axis label
46-
plt.xticks((-1, 0, 1), ('-1', '0', '1'), color='k', size=20)
35+
# Use tex in labels
36+
plt.xticks((-1, 0, 1), ('$-1$', r'$\pm 0$', '$+1$'), color='k', size=20)
4737

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)
38+
# Left Y-axis labels, combine math mode and text mode
39+
plt.ylabel(r'\bf{phase field} $\phi$', {'color': 'C0', 'fontsize': 20})
40+
plt.yticks((0, 0.5, 1), (r'\bf{0}', r'\bf{.5}', r'\bf{1}'), color='k', size=20)
5141

5242
# Right Y-axis labels
53-
plt.text(1.05, 0.5, r"\bf{level set} $\phi$", {'color': 'g', 'fontsize': 20},
43+
plt.text(1.02, 0.5, r"\bf{level set} $\phi$", {'color': 'C2', 'fontsize': 20},
5444
horizontalalignment='left',
5545
verticalalignment='center',
5646
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})
47+
clip_on=False,
48+
transform=plt.gca().transAxes)
6149

50+
# Use multiline environment inside a `text`.
6251
# 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})
52+
eq1 = r"\begin{eqnarray*}" + \
53+
r"|\nabla\phi| &=& 1,\\" + \
54+
r"\frac{\partial \phi}{\partial t} + U|\nabla \phi| &=& 0 " + \
55+
r"\end{eqnarray*}"
56+
plt.text(1, 0.9, eq1, {'color': 'C2', 'fontsize': 18}, va="top", ha="right")
6757

6858
# 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})
59+
eq2 = r'\begin{eqnarray*}' + \
60+
r'\mathcal{F} &=& \int f\left( \phi, c \right) dV, \\ ' + \
61+
r'\frac{ \partial \phi } { \partial t } &=& -M_{ \phi } ' + \
62+
r'\frac{ \delta \mathcal{F} } { \delta \phi }' + \
63+
r'\end{eqnarray*}'
64+
plt.text(0.18, 0.18, eq2, {'color': 'C0', 'fontsize': 16})
7465

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})
66+
plt.text(-1, .30, r'gamma: $\gamma$', {'color': 'r', 'fontsize': 20})
67+
plt.text(-1, .18, r'Omega: $\Omega$', {'color': 'b', 'fontsize': 20})
7868

7969
plt.show()

0 commit comments

Comments
 (0)