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

Skip to content

Commit 7f320b1

Browse files
author
Matthias Bussonnier
committed
DOC: update axes_demo to directly manipulate fig, ax
Directly call method on each individual axis; instead of the global ``plt.`` one, same to create inset; directly refer to the figure on which wee create an inset. I also use some more explicit renaming for each axis, and remove assignments on the LHS when those were unused as it seem like a distraction on the subject at hand.
1 parent 06491c0 commit 7f320b1

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

examples/subplots_axes_and_figures/axes_demo.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Axes Demo
44
=========
55
6-
Example use of ``plt.axes`` to create inset axes within the main plot axes.
6+
Example use of ``fig.add_axes`` to create inset axes within the main plot axes.
77
"""
88
import matplotlib.pyplot as plt
99
import numpy as np
@@ -19,27 +19,27 @@
1919
x = np.random.randn(len(t))
2020
s = np.convolve(x, r)[:len(x)] * dt # colored noise
2121

22-
# the main axes is subplot(111) by default
23-
plt.plot(t, s)
24-
plt.xlim(0, 1)
25-
plt.ylim(1.1 * np.min(s), 2 * np.max(s))
26-
plt.xlabel('time (s)')
27-
plt.ylabel('current (nA)')
28-
plt.title('Gaussian colored noise')
22+
fig, main_ax = plt.subplots()
23+
main_ax.plot(t, s)
24+
main_ax.set_xlim(0, 1)
25+
main_ax.set_ylim(1.1 * np.min(s), 2 * np.max(s))
26+
main_ax.set_xlabel('time (s)')
27+
main_ax.set_ylabel('current (nA)')
28+
main_ax.set_title('Gaussian colored noise')
2929

3030
# this is an inset axes over the main axes
31-
a = plt.axes([.65, .6, .2, .2], facecolor='k')
32-
n, bins, patches = plt.hist(s, 400, density=True)
33-
plt.title('Probability')
34-
plt.xticks([])
35-
plt.yticks([])
31+
right_inset_ax = fig.add_axes([.65, .6, .2, .2], facecolor='k')
32+
right_inset_ax.hist(s, 400, density=True)
33+
right_inset_ax.set_title('Probability')
34+
right_inset_ax.set_xticks([])
35+
right_inset_ax.set_yticks([])
3636

3737
# this is another inset axes over the main axes
38-
a = plt.axes([0.2, 0.6, .2, .2], facecolor='k')
39-
plt.plot(t[:len(r)], r)
40-
plt.title('Impulse response')
41-
plt.xlim(0, 0.2)
42-
plt.xticks([])
43-
plt.yticks([])
38+
left_inset_ax = fig.add_axes([.2, .6, .2, .2], facecolor='k')
39+
left_inset_ax.plot(t[:len(r)], r)
40+
left_inset_ax.set_title('Impulse response')
41+
left_inset_ax.set_xlim(0, 0.2)
42+
left_inset_ax.set_xticks([])
43+
left_inset_ax.set_yticks([])
4444

4545
plt.show()

0 commit comments

Comments
 (0)