|
14 | 14 | # Fixing random state for reproducibility |
15 | 15 | prng = np.random.RandomState(19680801) |
16 | 16 |
|
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. |
22 | 19 | 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]') |
25 | 41 |
|
26 | 42 | plt.show() |
0 commit comments