Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07f022d

Browse files
committed
DOC: update the api example engineering_formatter.py
1 parent 122cfa2 commit 07f022d

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

examples/api/engineering_formatter.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,29 @@
1414
# Fixing random state for reproducibility
1515
prng = np.random.RandomState(19680801)
1616

17-
fig, ax = plt.subplots()
18-
ax.set_xscale('log')
19-
formatter = EngFormatter(unit='Hz')
20-
ax.xaxis.set_major_formatter(formatter)
21-
17+
# Create artificial data to plot.
18+
# The x data span over several decades to demonstrate several SI prefixes.
2219
xs = np.logspace(1, 9, 100)
23-
ys = (0.8 + 0.4 * prng.uniform(size=100)) * np.log10(xs)**2
24-
ax.plot(xs, ys)
20+
ys = (0.8 + 0.4*prng.uniform(size=100))*np.log10(xs)**2
21+
22+
# Figure width is doubled (2*6.4) to display nicely 2 subplots side by side.
23+
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(12.8, 4.8))
24+
for ax in (ax0, ax1):
25+
ax.set_xscale('log')
26+
27+
# Demo of the default settings, with a user-defined unit label.
28+
ax0.set_title('Full unit ticklabels, w/ default precision & space sep.')
29+
formatter0 = EngFormatter(unit='Hz')
30+
ax0.xaxis.set_major_formatter(formatter0)
31+
ax0.plot(xs, ys)
32+
ax0.set_xlabel('Frequency')
33+
34+
# Demo of the options `places` (number of digit after decimal point) and
35+
# `space_sep` (presence of a space between the number and the prefix/unit).
36+
ax1.set_title('SI-prefix only ticklabels, 1-digit precision & w/o space sep.')
37+
formatter1 = EngFormatter(places=1, space_sep=False)
38+
ax1.xaxis.set_major_formatter(formatter1)
39+
ax1.plot(xs, ys)
40+
ax1.set_xlabel('Frequency [Hz]')
2541

2642
plt.show()

0 commit comments

Comments
 (0)