8000 DOC: Improve log scale example · matplotlib/matplotlib@6adabd3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6adabd3

Browse files
committed
DOC: Improve log scale example
- Remove https://matplotlib.org/stable/gallery/scales/semilogx_demo.html It is redundant to the other log scale example. - Explain that semilogx/semilogy/loglog are shortcuts. - Tune data/visualization
1 parent c11175d commit 6adabd3

File tree

2 files changed

+36
-55
lines changed

2 files changed

+36
-55
lines changed

galleries/examples/scales/log_demo.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
"""
2-
========
3-
Log Demo
4-
========
2+
=========
3+
Log scale
4+
=========
55
66
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)``.
715
"""
816

917
import matplotlib.pyplot as plt
1018
import numpy as np
1119

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')
20+
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, layout='constrained', figsize=(7, 7/3))
21+
# log x axis
22+
t = np.arange(0.01, 10.0, 0.01)
23+
ax1.semilogx(t, np.sin(2 * np.pi * t))
24+
ax1.set(title='semilogx')
2125
ax1.grid()
26+
ax1.grid(which="minor", color="0.9")
2227

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(4)
30+
ax2.semilogy(4*x, 10**x, 'o--')
31+
ax2.set(title='semilogy')
2632
ax2.grid()
33+
ax2.grid(which="minor", color="0.9")
2734

2835
# 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])
37+
ax3.loglog(x, 5 * x, 'o--')
38+
ax3.set(title='loglog')
3239
ax3.grid()
33-
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
38-
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)
45-
46-
fig.tight_layout()
40+
ax3.grid(which="minor", color="0.9")
41+
42+
# %%
43+
# Using the *base* parameter, one can set the base of the logarithm
44+
fig, ax = plt.subplots()
45+
ax.bar(["L1 cache", "L2 cache", "L3 cache", "RAM", "SSD"],
46+
[32, 1_000, 32_000, 16_000_000, 512_000_000])
47+
ax.set_yscale('log', base=2)
48+
ax.set_yticks([1, 2**10, 2**20, 2**30], labels=['kB', 'MB', 'GB', 'TB'])
49+
ax.set_title("Typical memory sizes")
50+
ax.yaxis.grid()
4751
plt.show()

galleries/examples/scales/semilogx_demo.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0