|
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 | +Convenience functions ``semilogx``, ``semilogy``, and ``loglog`` |
| 12 | +---------------------------------------------------------------- |
| 13 | +Since plotting data on semi-logarithmic or double-logarithmic scales is very common, |
| 14 | +the functions `~.Axes.semilogx`, `~.Axes.semilogy`, and `~.Axes.loglog` are shortcuts |
| 15 | +for setting the scale and plotting data; e.g. ``ax.semilogx(x, y)`` is equivalent to |
| 16 | +``ax.set_xscale('log'); ax.plot(x, y)``. |
7 | 17 | """
|
8 | 18 |
|
9 | 19 | import matplotlib.pyplot as plt
|
10 | 20 | import numpy as np
|
11 | 21 |
|
12 |
| -# Data for plotting |
13 |
| -t = np.arange(0.01, 20.0, 0.01) |
14 |
| - |
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 | +fig, (ax1, ax2, ax3) = plt.subplots(1, 3, layout='constrained', figsize=(7, 7/3)) |
| 23 | +# log x axis |
| 24 | +t = np.arange(0.01, 10.0, 0.01) |
| 25 | +ax1.semilogx(t, np.sin(2 * np.pi * t)) |
| 26 | +ax1.set(title='semilogx') |
21 | 27 | ax1.grid()
|
| 28 | +ax1.grid(which="minor", color="0.9") |
22 | 29 |
|
23 |
| -# log x axis |
24 |
| -ax2.semilogx(t, np.sin(2 * np.pi * t)) |
25 |
| -ax2.set(title='semilogx') |
| 30 | +# log y axis |
| 31 | +x = np.arange(4) |
| 32 | +ax2.semilogy(4*x, 10**x, 'o--') |
| 33 | +ax2.set(title='semilogy') |
26 | 34 | ax2.grid()
|
| 35 | +ax2.grid(which="minor", color="0.9") |
27 | 36 |
|
28 | 37 | # 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') |
| 38 | +x = np.array([1, 10, 100, 1000]) |
| 39 | +ax3.loglog(x, 5 * x, 'o--') |
| 40 | +ax3.set(title='loglog') |
32 | 41 | ax3.grid()
|
| 42 | +ax3.grid(which="minor", color="0.9") |
| 43 | + |
| 44 | +# %% |
| 45 | +# Logarithms with other bases |
| 46 | +# --------------------------- |
| 47 | +# By default, the log scale is to the base 10. One can change this via the *base* |
| 48 | +# parameter. |
| 49 | +fig, ax = plt.subplots() |
| 50 | +ax.bar(["L1 cache", "L2 cache", "L3 cache", "RAM", "SSD"], |
| 51 | + [32, 1_000, 32_000, 16_000_000, 512_000_000]) |
| 52 | +ax.set_yscale('log', base=2) |
| 53 | +ax.set_yticks([1, 2**10, 2**20, 2**30], labels=['kB', 'MB', 'GB', 'TB']) |
| 54 | +ax.set_title("Typical memory sizes") |
| 55 | +ax.yaxis.grid() |
| 56 | + |
| 57 | +# %% |
| 58 | +# Dealing with negative values |
| 59 | +# ---------------------------- |
| 60 | +# Non-positive values cannot be displayed on a log scale. The scale has two options |
| 61 | +# to handle these. Either mask the values so that they are ignored, or clip them |
| 62 | +# to a small positive value. Which one is more suited depends on the type of the |
| 63 | +# data and the visualization. |
| 64 | +# |
| 65 | +# The following example contains errorbars going negative. If we mask these values, |
| 66 | +# the bar vanishes, which is not desirable. In contrast, clipping makes the value |
| 67 | +# small positive (but well below the used scale) so that the error bar is drawn |
| 68 | +# to the edge of the Axes. |
| 69 | +x = np.linspace(0.0, 2.0, 10) |
| 70 | +y = 10**x |
| 71 | +yerr = 1.75 + 0.75*y |
33 | 72 |
|
34 |
| -# With errorbars: clip non-positive values |
35 |
| -# Use new data for plotting |
36 |
| -x = 10.0**np.linspace(0.0, 2.0, 20) |
37 |
| -y = x**2.0 |
| 73 | +fig, (ax1, ax2) = plt.subplots(1, 2, layout="constrained", figsize=(6, 3)) |
| 74 | +fig.suptitle("errorbars going negative") |
| 75 | +ax1.set_yscale("log", nonpositive='mask') |
| 76 | +ax1.set_title('nonpositive="mask"') |
| 77 | +ax1.errorbar(x, y, yerr=yerr, fmt='o', capsize=5) |
38 | 78 |
|
39 |
| -ax4.set_xscale("log", nonpositive='clip') |
40 |
| -ax4.set_yscale("log", nonpositive='clip') |
41 |
| -ax4.set(title='Errorbars go negative') |
42 |
| -ax4.errorbar(x, y, xerr=0.1 * x, yerr=5.0 + 0.75 * y) |
43 |
| -# ylim must be set after errorbar to allow errorbar to autoscale limits |
44 |
| -ax4.set_ylim(bottom=0.1) |
| 79 | +ax2.set_yscale("log", nonpositive='clip') |
| 80 | +ax2.set_title('nonpositive="clip"') |
| 81 | +ax2.errorbar(x, y, yerr=yerr, fmt='o', capsize=5) |
45 | 82 |
|
46 |
| -fig.tight_layout() |
47 | 83 | plt.show()
|
0 commit comments