From bd47d3d8651228f290952613ec8f94a76378f6e6 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 24 May 2023 17:17:43 +0200 Subject: [PATCH] Cleanup scalarformatter.py example. Move everything to a single figure for better comparability and remove repeated code. --- galleries/examples/ticks/scalarformatter.py | 96 ++++++--------------- 1 file changed, 25 insertions(+), 71 deletions(-) diff --git a/galleries/examples/ticks/scalarformatter.py b/galleries/examples/ticks/scalarformatter.py index 7dfae026c038..eaab5e9a86f2 100644 --- a/galleries/examples/ticks/scalarformatter.py +++ b/galleries/examples/ticks/scalarformatter.py @@ -3,82 +3,36 @@ The default tick formatter ========================== -The example shows use of the default `.ScalarFormatter` with different -settings. - -Example 1 : Default - -Example 2 : With no Numerical Offset - -Example 3 : With Mathtext +By default, tick labels are formatted using a `.ScalarFormatter`, which can be +configured via `~.axes.Axes.ticklabel_format`. This example illustrates some +possible configurations: + +- Default. +- ``useMathText=True``: Fancy formatting of mathematical expressions. +- ``useOffset=False``: Do not use offset notation; see + `.ScalarFormatter.set_useOffset`. """ import matplotlib.pyplot as plt import numpy as np -# %% -# Example 1 - x = np.arange(0, 1, .01) -fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) -fig.text(0.5, 0.975, 'Default settings', - horizontalalignment='center', - verticalalignment='top') - -ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) - -ax2.plot(x * 1e5, x * 1e-4) - -ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) - -ax4.plot(-x * 1e5, -x * 1e-4) - -fig.subplots_adjust(wspace=0.7, hspace=0.6) - -# %% -# Example 2 - -x = np.arange(0, 1, .01) -fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) -fig.text(0.5, 0.975, 'No numerical offset', - horizontalalignment='center', - verticalalignment='top') - -ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) -ax1.ticklabel_format(useOffset=False) - -ax2.plot(x * 1e5, x * 1e-4) -ax2.ticklabel_format(useOffset=False) - -ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) -ax3.ticklabel_format(useOffset=False) - -ax4.plot(-x * 1e5, -x * 1e-4) -ax4.ticklabel_format(useOffset=False) - -fig.subplots_adjust(wspace=0.7, hspace=0.6) - -# %% -# Example 3 - -x = np.arange(0, 1, .01) -fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) -fig.text(0.5, 0.975, 'With mathtext', - horizontalalignment='center', - verticalalignment='top') - -ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) -ax1.ticklabel_format(useMathText=True) - -ax2.plot(x * 1e5, x * 1e-4) -ax2.ticklabel_format(useMathText=True) - -ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) -ax3.ticklabel_format(useMathText=True) - -ax4.plot(-x * 1e5, -x * 1e-4) -ax4.ticklabel_format(useMathText=True) - -fig.subplots_adjust(wspace=0.7, hspace=0.6) +fig, axs = plt.subplots( + 3, 3, figsize=(9, 9), layout="constrained", gridspec_kw={"hspace": 0.1}) + +for col in axs.T: + col[0].plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) + col[1].plot(x * 1e5, x * 1e-4) + col[2].plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) + +for ax in axs[:, 1]: + ax.ticklabel_format(useMathText=True) +for ax in axs[:, 2]: + ax.ticklabel_format(useOffset=False) + +plt.rcParams.update({"axes.titleweight": "bold", "axes.titley": 1.1}) +axs[0, 0].set_title("default settings") +axs[0, 1].set_title("useMathText=True") +axs[0, 2].set_title("useOffset=False") plt.show()