|
1 | 1 | """ |
2 | | -======== |
3 | | -Log Demo |
4 | | -======== |
| 2 | +========= |
| 3 | +Log scale |
| 4 | +========= |
5 | 5 |
|
6 | 6 | Examples of plots with logarithmic axes. |
| 7 | +
|
| 8 | +You can set the x/y axes to be logarithmic by passing "log" to `~.Axes.set_xscale` / |
| 9 | +`~.Axes.set_yscale`. |
| 10 | +
|
| 11 | +Since plotting data on semi-logarithmic or double-logarithmic scales is very common, |
| 12 | +the functions `~.Axes.semilogx`, `~.Axes.semilogy`, and `~.Axes.loglog` are shortcuts |
| 13 | +for setting the scale and plotting data; e.g. ``ax.semilogx(x, y)`` is equivalent to |
| 14 | +``ax.set_xscale('log'); ax.plot(x, y)``. |
7 | 15 | """ |
8 | 16 |
|
9 | 17 | import matplotlib.pyplot as plt |
10 | 18 | import numpy as np |
11 | 19 |
|
12 | | -# Data for plotting |
13 | | -t = np.arange(0.01, 20.0, 0.01) |
| 20 | +fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, layout='constrained') |
14 | 21 |
|
15 | | -# Create figure |
16 | | -fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) |
17 | | - |
18 | | -# log y axis |
19 | | -ax1.semilogy(t, np.exp(-t / 5.0)) |
20 | | -ax1.set(title='semilogy') |
| 22 | +# log x axis |
| 23 | +t = np.arange(0.01, 10.0, 0.01) |
| 24 | +ax1.semilogx(t, np.sin(2 * np.pi * t)) |
| 25 | +ax1.set(title='semilogx') |
21 | 26 | ax1.grid() |
22 | 27 |
|
23 | | -# log x axis |
24 | | -ax2.semilogx(t, np.sin(2 * np.pi * t)) |
25 | | -ax2.set(title='semilogx') |
| 28 | +# log y axis |
| 29 | +x = np.arange(10) |
| 30 | +ax2.semilogy(x, 2**x, 'x:', base=2) |
| 31 | +ax2.set(title='semilogy with base 2') |
26 | 32 | ax2.grid() |
27 | 33 |
|
| 34 | + |
28 | 35 | # log x and y axis |
29 | | -ax3.loglog(t, 20 * np.exp(-t / 10.0)) |
30 | | -ax3.set_xscale('log', base=2) |
31 | | -ax3.set(title='loglog base 2 on x') |
| 36 | +x = np.array([1, 10, 100, 1000, 10000]) |
| 37 | +ax3.loglog(x, 5 * x, 'o--') |
| 38 | +ax3.set(title='loglog') |
32 | 39 | ax3.grid() |
33 | 40 |
|
34 | 41 | # With errorbars: clip non-positive values |
|
38 | 45 |
|
39 | 46 | ax4.set_xscale("log", nonpositive='clip') |
40 | 47 | ax4.set_yscale("log", nonpositive='clip') |
41 | | -ax4.set(title='Errorbars go negative') |
| 48 | +ax4.set(title='loglog with errorbars going negative') |
42 | 49 | ax4.errorbar(x, y, xerr=0.1 * x, yerr=5.0 + 0.75 * y) |
43 | 50 | # ylim must be set after errorbar to allow errorbar to autoscale limits |
44 | 51 | ax4.set_ylim(bottom=0.1) |
45 | 52 |
|
46 | | -fig.tight_layout() |
47 | 53 | plt.show() |
0 commit comments