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

Skip to content

Commit fb022d4

Browse files
committed
rewrote example to OO format
1 parent b781623 commit fb022d4

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

examples/pylab_examples/log_demo.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,41 @@
99
import numpy as np
1010
import matplotlib.pyplot as plt
1111

12-
plt.subplots_adjust(hspace=0.4)
12+
# Data for plotting
1313
t = np.arange(0.01, 20.0, 0.01)
1414

15+
# Create figure
16+
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
17+
18+
# Note hspace is the amount of height reserved for white space between subplots
19+
# expressed as a fraction of the average axis height
20+
fig.subplots_adjust(hspace=0.5)
21+
1522
# log y axis
16-
plt.subplot(221)
17-
plt.semilogy(t, np.exp(-t/5.0))
18-
plt.title('semilogy')
19-
plt.grid(True)
23+
ax1.semilogy(t, np.exp(-t/5.0))
24+
ax1.set(title='semilogy')
25+
ax1.grid()
2026

2127
# log x axis
22-
plt.subplot(222)
23-
plt.semilogx(t, np.sin(2*np.pi*t))
24-
plt.title('semilogx')
25-
plt.grid(True)
28+
ax2.semilogx(t, np.sin(2*np.pi*t))
29+
ax2.set(title='semilogx')
30+
ax2.grid()
2631

2732
# log x and y axis
28-
plt.subplot(223)
29-
plt.loglog(t, 20*np.exp(-t/10.0), basex=2)
30-
plt.grid(True)
31-
plt.title('loglog base 2 on x')
32-
33-
# with errorbars: clip non-positive values
34-
ax = plt.subplot(224)
35-
ax.set_xscale("log", nonposx='clip')
36-
ax.set_yscale("log", nonposy='clip')
33+
ax3.loglog(t, 20*np.exp(-t/10.0), basex=2)
34+
ax3.set(title='loglog base 2 on x')
35+
ax3.grid()
3736

37+
# With errorbars: clip non-positive values
38+
# Use new data for plotting
3839
x = 10.0**np.linspace(0.0, 2.0, 20)
3940
y = x**2.0
40-
plt.errorbar(x, y, xerr=0.1*x, yerr=5.0 + 0.75*y)
41-
ax.set_ylim(ymin=0.1)
42-
ax.set_title('Errorbars go negative')
4341

42+
ax4.set_xscale("log", nonposx='clip')
43+
ax4.set_yscale("log", nonposy='clip')
44+
ax4.set(title='Errorbars go negative')
45+
ax4.errorbar(x, y, xerr=0.1*x, yerr=5.0 + 0.75*y)
46+
# ylim must be set after errorbar to allow errorbar to autoscale limits
47+
ax4.set_ylim(ymin=0.1)
4448

4549
plt.show()

0 commit comments

Comments
 (0)