From f2e2aa02df60786f53f080cf58bf11698b67fc6c Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 29 May 2017 12:24:46 +0100 Subject: [PATCH 1/3] Move text examples out of pylab_examples PEP8 moved text examples Fix tutorial links Fix up rebase --- .../font_table_ttf_sgskip.py | 4 +- .../fonts_demo.py | 0 .../fonts_demo_kw.py | 0 .../mathtext_demo.py | 3 +- .../mathtext_examples.py | 17 +++---- .../stix_fonts_demo.py | 0 .../text_labels_and_annotations/tex_demo.py | 33 +++++++------ .../text_rotation_relative_to_line.py | 0 .../titles_demo.py | 0 .../usetex_baseline_test.py | 4 +- .../usetex_demo.py | 48 ++++++++++--------- tutorials/01_introductory/sample_plots.py | 4 +- 12 files changed, 59 insertions(+), 54 deletions(-) rename examples/{pylab_examples => text_labels_and_annotations}/font_table_ttf_sgskip.py (95%) rename examples/{pylab_examples => text_labels_and_annotations}/fonts_demo.py (100%) rename examples/{pylab_examples => text_labels_and_annotations}/fonts_demo_kw.py (100%) rename examples/{pylab_examples => text_labels_and_annotations}/mathtext_demo.py (92%) rename examples/{pylab_examples => text_labels_and_annotations}/mathtext_examples.py (90%) rename examples/{pylab_examples => text_labels_and_annotations}/stix_fonts_demo.py (100%) rename examples/{pylab_examples => text_labels_and_annotations}/text_rotation_relative_to_line.py (100%) rename examples/{pylab_examples => text_labels_and_annotations}/titles_demo.py (100%) rename examples/{pylab_examples => text_labels_and_annotations}/usetex_baseline_test.py (95%) rename examples/{pylab_examples => text_labels_and_annotations}/usetex_demo.py (61%) diff --git a/examples/pylab_examples/font_table_ttf_sgskip.py b/examples/text_labels_and_annotations/font_table_ttf_sgskip.py similarity index 95% rename from examples/pylab_examples/font_table_ttf_sgskip.py rename to examples/text_labels_and_annotations/font_table_ttf_sgskip.py index 9a2153d01d4c..880453b55089 100644 --- a/examples/pylab_examples/font_table_ttf_sgskip.py +++ b/examples/text_labels_and_annotations/font_table_ttf_sgskip.py @@ -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') diff --git a/examples/pylab_examples/fonts_demo.py b/examples/text_labels_and_annotations/fonts_demo.py similarity index 100% rename from examples/pylab_examples/fonts_demo.py rename to examples/text_labels_and_annotations/fonts_demo.py diff --git a/examples/pylab_examples/fonts_demo_kw.py b/examples/text_labels_and_annotations/fonts_demo_kw.py similarity index 100% rename from examples/pylab_examples/fonts_demo_kw.py rename to examples/text_labels_and_annotations/fonts_demo_kw.py diff --git a/examples/pylab_examples/mathtext_demo.py b/examples/text_labels_and_annotations/mathtext_demo.py similarity index 92% rename from examples/pylab_examples/mathtext_demo.py rename to examples/text_labels_and_annotations/mathtext_demo.py index 2dc02813f0cd..e2b03da638b9 100644 --- a/examples/pylab_examples/mathtext_demo.py +++ b/examples/text_labels_and_annotations/mathtext_demo.py @@ -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() diff --git a/examples/pylab_examples/mathtext_examples.py b/examples/text_labels_and_annotations/mathtext_examples.py similarity index 90% rename from examples/pylab_examples/mathtext_examples.py rename to examples/text_labels_and_annotations/mathtext_examples.py index 37776beec442..52e0319dc569 100644 --- a/examples/pylab_examples/mathtext_examples.py +++ b/examples/text_labels_and_annotations/mathtext_examples.py @@ -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) + 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)) @@ -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 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) @@ -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. diff --git a/examples/pylab_examples/stix_fonts_demo.py b/examples/text_labels_and_annotations/stix_fonts_demo.py similarity index 100% rename from examples/pylab_examples/stix_fonts_demo.py rename to examples/text_labels_and_annotations/stix_fonts_demo.py diff --git a/examples/text_labels_and_annotations/tex_demo.py b/examples/text_labels_and_annotations/tex_demo.py index 417bbc63d69b..f5db743b7889 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 -================================= +======== +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 @@ -9,27 +9,26 @@ 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 """ -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() diff --git a/examples/pylab_examples/text_rotation_relative_to_line.py b/examples/text_labels_and_annotations/text_rotation_relative_to_line.py similarity index 100% rename from examples/pylab_examples/text_rotation_relative_to_line.py rename to examples/text_labels_and_annotations/text_rotation_relative_to_line.py diff --git a/examples/pylab_examples/titles_demo.py b/examples/text_labels_and_annotations/titles_demo.py similarity index 100% rename from examples/pylab_examples/titles_demo.py rename to examples/text_labels_and_annotations/titles_demo.py diff --git a/examples/pylab_examples/usetex_baseline_test.py b/examples/text_labels_and_annotations/usetex_baseline_test.py similarity index 95% rename from examples/pylab_examples/usetex_baseline_test.py rename to examples/text_labels_and_annotations/usetex_baseline_test.py index d626f660af7f..1a222b61a912 100644 --- a/examples/pylab_examples/usetex_baseline_test.py +++ b/examples/text_labels_and_annotations/usetex_baseline_test.py @@ -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) @@ -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")) @@ -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], diff --git a/examples/pylab_examples/usetex_demo.py b/examples/text_labels_and_annotations/usetex_demo.py similarity index 61% rename from examples/pylab_examples/usetex_demo.py rename to examples/text_labels_and_annotations/usetex_demo.py index 7a0df3b4c5aa..c3e1ee7a4cb0 100644 --- a/examples/pylab_examples/usetex_demo.py +++ b/examples/text_labels_and_annotations/usetex_demo.py @@ -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) @@ -32,10 +32,14 @@ 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 @@ -43,31 +47,31 @@ # Left Y-axis labels plt.ylabel(r'\bf{phase field} $\phi$', {'color': 'b', - 'fontsize': 20}) + 'fontsize': 20}) 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}) diff --git a/tutorials/01_introductory/sample_plots.py b/tutorials/01_introductory/sample_plots.py index bc9bb9f12276..c4fedcbc4cbd 100644 --- a/tutorials/01_introductory/sample_plots.py +++ b/tutorials/01_introductory/sample_plots.py @@ -319,8 +319,8 @@ and the DejaVu, BaKoMa computer modern, or `STIX `_ 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 From 048cafd2c17219d0e47d90aa073f90d516902a02 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 30 May 2017 10:04:03 +0100 Subject: [PATCH 2/3] Revert changes to text_demo --- .../text_labels_and_annotations/tex_demo.py | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/examples/text_labels_and_annotations/tex_demo.py b/examples/text_labels_and_annotations/tex_demo.py index f5db743b7889..417bbc63d69b 100644 --- a/examples/text_labels_and_annotations/tex_demo.py +++ b/examples/text_labels_and_annotations/tex_demo.py @@ -1,7 +1,7 @@ """ -======== -Tex Demo -======== +================================= +Rendering math equation 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 @@ -9,26 +9,27 @@ 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 in -~/.tex.cache +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! """ +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) -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') +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.show() From dac08ce9a95ce4d3b95cc403b9131b0353d43176 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 30 May 2017 13:38:06 +0100 Subject: [PATCH 3/3] Minor fixes --- examples/text_labels_and_annotations/mathtext_examples.py | 8 ++++---- examples/text_labels_and_annotations/usetex_demo.py | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/text_labels_and_annotations/mathtext_examples.py b/examples/text_labels_and_annotations/mathtext_examples.py index 52e0319dc569..86c349f10d11 100644 --- a/examples/text_labels_and_annotations/mathtext_examples.py +++ b/examples/text_labels_and_annotations/mathtext_examples.py @@ -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.) + 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)) @@ -86,7 +86,7 @@ def doall(): # 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_next = baseline - line_axesfrac title = mathtext_titles[i_line] + ":" fill_color = ['white', mpl_blue_rvb][i_line % 2] plt.fill_between([0., 1.], [baseline, baseline], diff --git a/examples/text_labels_and_annotations/usetex_demo.py b/examples/text_labels_and_annotations/usetex_demo.py index c3e1ee7a4cb0..86d4587c6dc4 100644 --- a/examples/text_labels_and_annotations/usetex_demo.py +++ b/examples/text_labels_and_annotations/usetex_demo.py @@ -46,8 +46,7 @@ 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}) +plt.ylabel(r'\bf{phase field} $\phi$', {'color': 'b', 'fontsize': 20}) plt.yticks((0, 0.5, 1), ('0', '.5', '1'), color='k', size=20) # Right Y-axis labels