|
3 | 3 | The default tick formatter |
4 | 4 | ========================== |
5 | 5 |
|
6 | | -The example shows use of the default `.ScalarFormatter` with different |
7 | | -settings. |
8 | | -
|
9 | | -Example 1 : Default |
10 | | -
|
11 | | -Example 2 : With no Numerical Offset |
12 | | -
|
13 | | -Example 3 : With Mathtext |
| 6 | +By default, tick labels are formatted using a `.ScalarFormatter`, which can be |
| 7 | +configured via `~.axes.Axes.ticklabel_format`. This example illustrates some |
| 8 | +possible configurations: |
| 9 | +
|
| 10 | +- Default. |
| 11 | +- ``useMathText=True``: Fancy formatting of mathematical expressions. |
| 12 | +- ``useOffset=False``: Do not use offset notation; see |
| 13 | + `.ScalarFormatter.set_useOffset`. |
14 | 14 | """ |
15 | 15 |
|
16 | 16 | import matplotlib.pyplot as plt |
17 | 17 | import numpy as np |
18 | 18 |
|
19 | | -# %% |
20 | | -# Example 1 |
21 | | - |
22 | 19 | x = np.arange(0, 1, .01) |
23 | | -fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) |
24 | | -fig.text(0.5, 0.975, 'Default settings', |
25 | | - horizontalalignment='center', |
26 | | - verticalalignment='top') |
27 | | - |
28 | | -ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) |
29 | | - |
30 | | -ax2.plot(x * 1e5, x * 1e-4) |
31 | | - |
32 | | -ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) |
33 | | - |
34 | | -ax4.plot(-x * 1e5, -x * 1e-4) |
35 | | - |
36 | | -fig.subplots_adjust(wspace=0.7, hspace=0.6) |
37 | | - |
38 | | -# %% |
39 | | -# Example 2 |
40 | | - |
41 | | -x = np.arange(0, 1, .01) |
42 | | -fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) |
43 | | -fig.text(0.5, 0.975, 'No numerical offset', |
44 | | - horizontalalignment='center', |
45 | | - verticalalignment='top') |
46 | | - |
47 | | -ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) |
48 | | -ax1.ticklabel_format(useOffset=False) |
49 | | - |
50 | | -ax2.plot(x * 1e5, x * 1e-4) |
51 | | -ax2.ticklabel_format(useOffset=False) |
52 | | - |
53 | | -ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) |
54 | | -ax3.ticklabel_format(useOffset=False) |
55 | | - |
56 | | -ax4.plot(-x * 1e5, -x * 1e-4) |
57 | | -ax4.ticklabel_format(useOffset=False) |
58 | | - |
59 | | -fig.subplots_adjust(wspace=0.7, hspace=0.6) |
60 | | - |
61 | | -# %% |
62 | | -# Example 3 |
63 | | - |
64 | | -x = np.arange(0, 1, .01) |
65 | | -fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) |
66 | | -fig.text(0.5, 0.975, 'With mathtext', |
67 | | - horizontalalignment='center', |
68 | | - verticalalignment='top') |
69 | | - |
70 | | -ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) |
71 | | -ax1.ticklabel_format(useMathText=True) |
72 | | - |
73 | | -ax2.plot(x * 1e5, x * 1e-4) |
74 | | -ax2.ticklabel_format(useMathText=True) |
75 | | - |
76 | | -ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) |
77 | | -ax3.ticklabel_format(useMathText=True) |
78 | | - |
79 | | -ax4.plot(-x * 1e5, -x * 1e-4) |
80 | | -ax4.ticklabel_format(useMathText=True) |
81 | | - |
82 | | -fig.subplots_adjust(wspace=0.7, hspace=0.6) |
| 20 | +fig, axs = plt.subplots( |
| 21 | + 3, 3, figsize=(9, 9), layout="constrained", gridspec_kw={"hspace": 0.1}) |
| 22 | + |
| 23 | +for col in axs.T: |
| 24 | + col[0].plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) |
| 25 | + col[1].plot(x * 1e5, x * 1e-4) |
| 26 | + col[2].plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) |
| 27 | + |
| 28 | +for ax in axs[:, 1]: |
| 29 | + ax.ticklabel_format(useMathText=True) |
| 30 | +for ax in axs[:, 2]: |
| 31 | + ax.ticklabel_format(useOffset=False) |
| 32 | + |
| 33 | +plt.rcParams.update({"axes.titleweight": "bold", "axes.titley": 1.1}) |
| 34 | +axs[0, 0].set_title("default settings") |
| 35 | +axs[0, 1].set_title("useMathText=True") |
| 36 | +axs[0, 2].set_title("useOffset=False") |
83 | 37 |
|
84 | 38 | plt.show() |
0 commit comments