Skip to content

Commit 5cccf04

Browse files
committed
Cleanup scalarformatter.py example.
Move everything to a single figure for better comparability and remove repeated code.
1 parent ffd3b12 commit 5cccf04

File tree

1 file changed

+19
-71
lines changed

1 file changed

+19
-71
lines changed

galleries/examples/ticks/scalarformatter.py

Lines changed: 19 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,30 @@
33
The default tick formatter
44
==========================
55
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+
The example shows use of the default `.ScalarFormatter` with three different
7+
settings: default, with mathtext, and with no numerical offset.
148
"""
159

1610
import matplotlib.pyplot as plt
1711
import numpy as np
1812

19-
# %%
20-
# Example 1
21-
2213
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)
14+
fig, axs = plt.subplots(
15+
3, 3, figsize=(9, 9), layout="constrained", gridspec_kw={"hspace": 0.1})
16+
17+
for col in axs.T:
18+
col[0].plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5)
19+
col[1].plot(x * 1e5, x * 1e-4)
20+
col[2].plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10)
21+
22+
for ax in axs[:, 1]:
23+
ax.ticklabel_format(useMathText=True)
24+
for ax in axs[:, 2]:
25+
ax.ticklabel_format(useOffset=False)
26+
27+
plt.rcParams.update({"axes.titleweight": "bold", "axes.titley": 1.1})
28+
axs[0, 0].set_title("default settings")
29+
axs[0, 1].set_title("with mathtext")
30+
axs[0, 2].set_title("no numerical offset")
8331

8432
plt.show()

0 commit comments

Comments
 (0)