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

Skip to content

Commit bbd2db6

Browse files
authored
Merge pull request #14951 from meeseeksmachine/auto-backport-of-pr-14934-on-v3.1.x
Backport PR #14934 on branch v3.1.x (DOC: update axes_demo to directly manipulate fig, ax)
2 parents 1d1ffc9 + 4be8b3b commit bbd2db6

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

examples/subplots_axes_and_figures/axes_demo.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
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.
7+
8+
Please see also the :ref:`axes_grid_examples` section, and the following three
9+
examples:
10+
11+
- :doc:`/gallery/subplots_axes_and_figures/zoom_inset_axes`
12+
- :doc:`/gallery/axes_grid1/inset_locator_demo`
13+
- :doc:`/gallery/axes_grid1/inset_locator_demo2`
714
"""
815
import matplotlib.pyplot as plt
916
import numpy as np
@@ -19,27 +26,27 @@
1926
x = np.random.randn(len(t))
2027
s = np.convolve(x, r)[:len(x)] * dt # colored noise
2128

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')
29+
fig, main_ax = plt.subplots()
30+
main_ax.plot(t, s)
31+
main_ax.set_xlim(0, 1)
32+
main_ax.set_ylim(1.1 * np.min(s), 2 * np.max(s))
33+
main_ax.set_xlabel('time (s)')
34+
main_ax.set_ylabel('current (nA)')
35+
main_ax.set_title('Gaussian colored noise')
2936

3037
# 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([])
38+
right_inset_ax = fig.add_axes([.65, .6, .2, .2], facecolor='k')
39+
right_inset_ax.hist(s, 400, density=True)
40+
right_inset_ax.set_title('Probability')
41+
right_inset_ax.set_xticks([])
42+
right_inset_ax.set_yticks([])
3643

3744
# 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([])
45+
left_inset_ax = fig.add_axes([.2, .6, .2, .2], facecolor='k')
46+
left_inset_ax.plot(t[:len(r)], r)
47+
left_inset_ax.set_title('Impulse response')
48+
left_inset_ax.set_xlim(0, 0.2)
49+
left_inset_ax.set_xticks([])
50+
left_inset_ax.set_yticks([])
4451

4552
plt.show()

0 commit comments

Comments
 (0)