From fe6c45c95f61354a2cad75bd15f42292239874d7 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 16 Dec 2019 13:37:59 +0100 Subject: [PATCH] Merge the two usetex demos. They're demonstrating the same idea. --- .../text_labels_and_annotations/tex_demo.py | 66 +++++++++++++++++- .../usetex_demo.py | 68 ------------------- 2 files changed, 63 insertions(+), 71 deletions(-) delete mode 100644 examples/text_labels_and_annotations/usetex_demo.py diff --git a/examples/text_labels_and_annotations/tex_demo.py b/examples/text_labels_and_annotations/tex_demo.py index f8683a825795..888462ca0052 100644 --- a/examples/text_labels_and_annotations/tex_demo.py +++ b/examples/text_labels_and_annotations/tex_demo.py @@ -1,7 +1,7 @@ """ -================================= -Rendering math equation using TeX -================================= +================================== +Rendering math equations using TeX +================================== You can use TeX to render all of your matplotlib text if the rc parameter ``text.usetex`` is set. This works currently on the agg and ps @@ -30,4 +30,64 @@ ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16) ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty' r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r') + +############################################################################# +# A more complex example. + +fig, ax = plt.subplots() +# interface tracking profiles +N = 500 +delta = 0.6 +X = np.linspace(-1, 1, N) +ax.plot(X, (1 - np.tanh(4 * X / delta)) / 2, # phase field tanh profiles + X, (1.4 + np.tanh(4 * X / delta)) / 4, "C2", # composition profile + X, X < 0, "k--") # sharp interface + +# legend +ax.legend(("phase field", "level set", "sharp interface"), + shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=16) + +# the arrow +ax.annotate("", xy=(-delta / 2., 0.1), xytext=(delta / 2., 0.1), + arrowprops=dict(arrowstyle="<->", connectionstyle="arc3")) +ax.text(0, 0.1, r"$\delta$", + color="black", fontsize=24, + horizontalalignment="center", verticalalignment="center", + bbox=dict(boxstyle="round", fc="white", ec="black", pad=0.2)) + +# Use tex in labels +ax.set_xticks([-1, 0, 1]) +ax.set_xticklabels(["$-1$", r"$\pm 0$", "$+1$"], color="k", size=20) + +# Left Y-axis labels, combine math mode and text mode +ax.set_ylabel(r"\bf{phase field} $\phi$", color="C0", fontsize=20) +ax.set_yticks([0, 0.5, 1]) +ax.set_yticklabels([r"\bf{0}", r"\bf{.5}", r"\bf{1}"], color="k", size=20) + +# Right Y-axis labels +ax.text(1.02, 0.5, r"\bf{level set} $\phi$", + color="C2", fontsize=20, rotation=90, + horizontalalignment="left", verticalalignment="center", + clip_on=False, transform=ax.transAxes) + +# Use multiline environment inside a `text`. +# level set equations +eq1 = (r"\begin{eqnarray*}" + r"|\nabla\phi| &=& 1,\\" + r"\frac{\partial \phi}{\partial t} + U|\nabla \phi| &=& 0 " + r"\end{eqnarray*}") +ax.text(1, 0.9, eq1, color="C2", fontsize=18, + horizontalalignment="right", verticalalignment="top") + +# phase field equations +eq2 = (r"\begin{eqnarray*}" + r"\mathcal{F} &=& \int f\left( \phi, c \right) dV, \\ " + r"\frac{ \partial \phi } { \partial t } &=& -M_{ \phi } " + r"\frac{ \delta \mathcal{F} } { \delta \phi }" + r"\end{eqnarray*}") +ax.text(0.18, 0.18, eq2, color="C0", fontsize=16) + +ax.text(-1, .30, r"gamma: $\gamma$", color="r", fontsize=20) +ax.text(-1, .18, r"Omega: $\Omega$", color="b", fontsize=20) + plt.show() diff --git a/examples/text_labels_and_annotations/usetex_demo.py b/examples/text_labels_and_annotations/usetex_demo.py deleted file mode 100644 index 09902ad9efe1..000000000000 --- a/examples/text_labels_and_annotations/usetex_demo.py +++ /dev/null @@ -1,68 +0,0 @@ -""" -=========== -Usetex Demo -=========== - -Shows how to use latex in a plot. - -Also refer to the :doc:`/tutorials/text/usetex` guide. -""" - -import numpy as np -import matplotlib.pyplot as plt -plt.rc('text', usetex=True) - -# interface tracking profiles -N = 500 -delta = 0.6 -X = np.linspace(-1, 1, N) -plt.plot(X, (1 - np.tanh(4 * X / delta)) / 2, # phase field tanh profiles - X, (1.4 + np.tanh(4 * X / delta)) / 4, "C2", # composition profile - X, X < 0, 'k--') # sharp interface - -# legend -plt.legend(('phase field', 'level set', 'sharp interface'), - shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=16) - -# the arrow -plt.annotate("", xy=(-delta / 2., 0.1), xytext=(delta / 2., 0.1), - arrowprops=dict(arrowstyle="<->", connectionstyle="arc3")) -plt.text(0, 0.1, r'$\delta$', - {'color': 'black', 'fontsize': 24, 'ha': 'center', 'va': 'center', - 'bbox': dict(boxstyle="round", fc="white", ec="black", pad=0.2)}) - -# Use tex in labels -plt.xticks((-1, 0, 1), ('$-1$', r'$\pm 0$', '$+1$'), color='k', size=20) - -# Left Y-axis labels, combine math mode and text mode -plt.ylabel(r'\bf{phase field} $\phi$', {'color': 'C0', 'fontsize': 20}) -plt.yticks((0, 0.5, 1), (r'\bf{0}', r'\bf{.5}', r'\bf{1}'), color='k', size=20) - -# Right Y-axis labels -plt.text(1.02, 0.5, r"\bf{level set} $\phi$", {'color': 'C2', 'fontsize': 20}, - horizontalalignment='left', - verticalalignment='center', - rotation=90, - clip_on=False, - transform=plt.gca().transAxes) - -# Use multiline environment inside a `text`. -# level set equations -eq1 = r"\begin{eqnarray*}" + \ - r"|\nabla\phi| &=& 1,\\" + \ - r"\frac{\partial \phi}{\partial t} + U|\nabla \phi| &=& 0 " + \ - r"\end{eqnarray*}" -plt.text(1, 0.9, eq1, {'color': 'C2', 'fontsize': 18}, va="top", ha="right") - -# phase field equations -eq2 = r'\begin{eqnarray*}' + \ - r'\mathcal{F} &=& \int f\left( \phi, c \right) dV, \\ ' + \ - r'\frac{ \partial \phi } { \partial t } &=& -M_{ \phi } ' + \ - r'\frac{ \delta \mathcal{F} } { \delta \phi }' + \ - r'\end{eqnarray*}' -plt.text(0.18, 0.18, eq2, {'color': 'C0', 'fontsize': 16}) - -plt.text(-1, .30, r'gamma: $\gamma$', {'color': 'r', 'fontsize': 20}) -plt.text(-1, .18, r'Omega: $\Omega$', {'color': 'b', 'fontsize': 20}) - -plt.show()