Skip to content

Move text examples out of pylab_examples #8681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move text examples out of pylab_examples
PEP8 moved text examples

Fix tutorial links

Fix up rebase
  • Loading branch information
dstansby committed May 30, 2017
commit f2e2aa02df60786f53f080cf58bf11698b67fc6c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
tab = plt.table(cellText=chars,
rowLabels=labelr,
colLabels=labelc,
rowColours=[lightgrn]*16,
colColours=[lightgrn]*16,
rowColours=[lightgrn] * 16,
colColours=[lightgrn] * 16,
cellColours=colors,
cellLoc='center',
loc='upper left')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

ax.legend([r"$\sqrt{x^2}$"])

ax.set_title(r'$\Delta_i^j \hspace{0.4} \mathrm{versus} \hspace{0.4} \Delta_{i+1}^j$', fontsize=20)
ax.set_title(r'$\Delta_i^j \hspace{0.4} \mathrm{versus} \hspace{0.4} '
r'\Delta_{i+1}^j$', fontsize=20)

show()
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@

def doall():
# Colors used in mpl online documentation.
mpl_blue_rvb = (191./255., 209./256., 212./255.)
mpl_orange_rvb = (202/255., 121/256., 0./255.)
mpl_grey_rvb = (51./255., 51./255., 51./255.)
mpl_blue_rvb = (191 / 255, 209 / 256, 212 / 255)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs from __future__ import division for this to work on Python 2.

mpl_orange_rvb = (202 / 255, 121 / 256, 0 / 255)
mpl_grey_rvb = (51 / 255, 51 / 255, 51 / 255)

# Creating figure and axis.
plt.figure(figsize=(6, 7))
Expand All @@ -79,25 +79,25 @@ def doall():
# Plotting header demonstration formula
full_demo = mathext_demos[0]
plt.annotate(full_demo,
xy=(0.5, 1. - 0.59*line_axesfrac),
xy=(0.5, 1. - 0.59 * line_axesfrac),
xycoords='data', color=mpl_orange_rvb, ha='center',
fontsize=20)

# Plotting features demonstration formulae
for i_line in range(1, n_lines):
baseline = 1. - (i_line)*line_axesfrac
baseline_next = baseline - line_axesfrac*1.
baseline = 1 - (i_line) * line_axesfrac
baseline_next = baseline - line_axesfrac * 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* 1 does nothing?

title = mathtext_titles[i_line] + ":"
fill_color = ['white', mpl_blue_rvb][i_line % 2]
plt.fill_between([0., 1.], [baseline, baseline],
[baseline_next, baseline_next],
color=fill_color, alpha=0.5)
plt.annotate(title,
xy=(0.07, baseline - 0.3*line_axesfrac),
xy=(0.07, baseline - 0.3 * line_axesfrac),
xycoords='data', color=mpl_grey_rvb, weight='bold')
demo = mathext_demos[i_line]
plt.annotate(demo,
xy=(0.05, baseline - 0.75*line_axesfrac),
xy=(0.05, baseline - 0.75 * line_axesfrac),
xycoords='data', color=mpl_grey_rvb,
fontsize=16)

Expand All @@ -106,6 +106,7 @@ def doall():
print(i, s)
plt.show()


if '--latex' in sys.argv:
# Run: python mathtext_examples.py --latex
# Need amsmath and amssymb packages.
Expand Down
33 changes: 16 additions & 17 deletions examples/text_labels_and_annotations/tex_demo.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
"""
=================================
Rendering math equation using TeX
=================================
========
Tex Demo
========

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
backends, and requires that you have tex and the other dependencies
described at http://matplotlib.org/users/usetex.html
properly installed on your system. The first time you run a script
you will see a lot of output from tex and associated tools. The next
time, the run may be silent, as a lot of the information is cached.

Notice how the the label for the y axis is provided using unicode!
time, the run may be silent, as a lot of the information is cached in
~/.tex.cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be here; I think you may have resolved conflicts in the opposite direction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just put back in the master copy of tex_demo.py, which I think is correct.


"""
from __future__ import unicode_literals
import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.unicode'] = True
import matplotlib.pyplot as plt


plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.figure(1, figsize=(6, 4))
ax = plt.axes([0.1, 0.1, 0.8, 0.7])
t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2
plt.plot(t, s)

fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)

ax.set_xlabel(r'\textbf{time (s)}')
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')
plt.xlabel(r'\textbf{time (s)}')
plt.ylabel(r'\textit{voltage (mV)}', fontsize=16)
plt.title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty"
r"\frac{-e^{i\pi}}{2^n}$!", fontsize=16, color='r')
plt.grid(True)
plt.savefig('tex_demo')
plt.show()
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def draw(self, renderer):
plt.rcParams["text.usetex"] = usetex
plt.rcParams["text.latex.preview"] = preview


subplot = maxes.subplot_class_factory(Axes)


Expand All @@ -46,7 +47,6 @@ def test_window_extent(ax, usetex, preview):
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

#t = ax.text(0., 0., r"mlp", va="baseline", size=150)
text_kw = dict(va=va,
size=50,
bbox=dict(pad=0., ec="k", fc="none"))
Expand All @@ -67,7 +67,7 @@ def test_window_extent(ax, usetex, preview):
ax.set_title("usetex=%s\npreview=%s" % (str(usetex), str(preview)))


fig = plt.figure(figsize=(2.*3, 6.5))
fig = plt.figure(figsize=(2 * 3, 6.5))

for i, usetex, preview in [[0, False, False],
[1, True, False],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
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, (X + 1)/2, # level set distance function
X, (1.4 + np.tanh(4.*X/delta))/4, # composition profile
X, X < 0, 'k--', # sharp interface
)
plt.plot(X, (1 - np.tanh(4 * X / delta)) / 2, # phase field tanh profiles
X, (X + 1) / 2, # level set distance function
X, (1.4 + np.tanh(4 * X / delta)) / 4, # composition profile
X, X < 0, 'k--') # sharp interface

# legend
plt.legend(('phase field', 'level set', 'composition', 'sharp interface'), shadow=True, loc=(0.01, 0.55))
plt.legend(('phase field', 'level set', 'composition', 'sharp interface'),
shadow=True, loc=(0.01, 0.55))

ltext = plt.gca().get_legend().get_texts()
plt.setp(ltext[0], fontsize=20)
Expand All @@ -32,42 +32,46 @@
height = 0.1
offset = 0.02
plt.plot((-delta / 2., delta / 2), (height, height), 'k', linewidth=2)
plt.plot((-delta / 2, -delta / 2 + offset * 2), (height, height - offset), 'k', linewidth=2)
plt.plot((-delta / 2, -delta / 2 + offset * 2), (height, height + offset), 'k', linewidth=2)
plt.plot((delta / 2, delta / 2 - offset * 2), (height, height - offset), 'k', linewidth=2)
plt.plot((delta / 2, delta / 2 - offset * 2), (height, height + offset), 'k', linewidth=2)
plt.plot((-delta / 2, -delta / 2 + offset * 2), (height, height - offset),
'k', linewidth=2)
plt.plot((-delta / 2, -delta / 2 + offset * 2), (height, height + offset),
'k', linewidth=2)
plt.plot((delta / 2, delta / 2 - offset * 2), (height, height - offset),
'k', linewidth=2)
plt.plot((delta / 2, delta / 2 - offset * 2), (height, height + offset),
'k', linewidth=2)
plt.text(-0.06, height - 0.06, r'$\delta$', {'color': 'k', 'fontsize': 24})

# X-axis label
plt.xticks((-1, 0, 1), ('-1', '0', '1'), color='k', size=20)

# Left Y-axis labels
plt.ylabel(r'\bf{phase field} $\phi$', {'color': 'b',
'fontsize': 20})
'fontsize': 20})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well break before the {.

plt.yticks((0, 0.5, 1), ('0', '.5', '1'), color='k', size=20)

# Right Y-axis labels
plt.text(1.05, 0.5, r"\bf{level set} $\phi$", {'color': 'g', 'fontsize': 20},
horizontalalignment='left',
verticalalignment='center',
rotation=90,
clip_on=False)
horizontalalignment='left',
verticalalignment='center',
rotation=90,
clip_on=False)
plt.text(1.01, -0.02, "-1", {'color': 'k', 'fontsize': 20})
plt.text(1.01, 0.98, "1", {'color': 'k', 'fontsize': 20})
plt.text(1.01, 0.48, "0", {'color': 'k', 'fontsize': 20})

# level set equations
plt.text(0.1, 0.85,
r'$|\nabla\phi| = 1,$ \newline $ \frac{\partial \phi}{\partial t}'
r'+ U|\nabla \phi| = 0$',
{'color': 'g', 'fontsize': 20})
r'$|\nabla\phi| = 1,$ \newline $ \frac{\partial \phi}{\partial t}'
r'+ U|\nabla \phi| = 0$',
{'color': 'g', 'fontsize': 20})

# phase field equations
plt.text(0.2, 0.15,
r'$\mathcal{F} = \int f\left( \phi, c \right) dV,$ \newline '
r'$ \frac{ \partial \phi } { \partial t } = -M_{ \phi } '
r'\frac{ \delta \mathcal{F} } { \delta \phi }$',
{'color': 'b', 'fontsize': 20})
r'$\mathcal{F} = \int f\left( \phi, c \right) dV,$ \newline '
r'$ \frac{ \partial \phi } { \partial t } = -M_{ \phi } '
r'\frac{ \delta \mathcal{F} } { \delta \phi }$',
{'color': 'b', 'fontsize': 20})

# these went wrong in pdf in a previous version
plt.text(-.9, .42, r'gamma: $\gamma$', {'color': 'r', 'fontsize': 20})
Expand Down
4 changes: 2 additions & 2 deletions tutorials/01_introductory/sample_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@
and the DejaVu, BaKoMa computer modern, or `STIX <http://www.stixfonts.org>`_
fonts. See the :mod:`matplotlib.mathtext` module for additional details.

.. figure:: ../../gallery/pylab_examples/images/sphx_glr_mathtext_examples_001.png
:target: ../../gallery/pylab_examples/mathtext_examples.html
.. figure:: ../../gallery/text_labels_and_annotations/images/sphx_glr_mathtext_examples_001.png
:target: ../../gallery/text_labels_and_annotations/mathtext_examples.html
:align: center
:scale: 50

Expand Down