diff --git a/examples/subplots_axes_and_figures/axes_demo.py b/examples/subplots_axes_and_figures/axes_demo.py index 5b07f2f2d820..972567420434 100644 --- a/examples/subplots_axes_and_figures/axes_demo.py +++ b/examples/subplots_axes_and_figures/axes_demo.py @@ -3,7 +3,14 @@ Axes Demo ========= -Example use of ``plt.axes`` to create inset axes within the main plot axes. +Example use of ``fig.add_axes`` to create inset axes within the main plot axes. + +Please see also the :ref:`axes_grid_examples` section, and the following three +examples: + + - :doc:`/gallery/subplots_axes_and_figures/zoom_inset_axes` + - :doc:`/gallery/axes_grid1/inset_locator_demo` + - :doc:`/gallery/axes_grid1/inset_locator_demo2` """ import matplotlib.pyplot as plt import numpy as np @@ -19,27 +26,27 @@ x = np.random.randn(len(t)) s = np.convolve(x, r)[:len(x)] * dt # colored noise -# the main axes is subplot(111) by default -plt.plot(t, s) -plt.xlim(0, 1) -plt.ylim(1.1 * np.min(s), 2 * np.max(s)) -plt.xlabel('time (s)') -plt.ylabel('current (nA)') -plt.title('Gaussian colored noise') +fig, main_ax = plt.subplots() +main_ax.plot(t, s) +main_ax.set_xlim(0, 1) +main_ax.set_ylim(1.1 * np.min(s), 2 * np.max(s)) +main_ax.set_xlabel('time (s)') +main_ax.set_ylabel('current (nA)') +main_ax.set_title('Gaussian colored noise') # this is an inset axes over the main axes -a = plt.axes([.65, .6, .2, .2], facecolor='k') -n, bins, patches = plt.hist(s, 400, density=True) -plt.title('Probability') -plt.xticks([]) -plt.yticks([]) +right_inset_ax = fig.add_axes([.65, .6, .2, .2], facecolor='k') +right_inset_ax.hist(s, 400, density=True) +right_inset_ax.set_title('Probability') +right_inset_ax.set_xticks([]) +right_inset_ax.set_yticks([]) # this is another inset axes over the main axes -a = plt.axes([0.2, 0.6, .2, .2], facecolor='k') -plt.plot(t[:len(r)], r) -plt.title('Impulse response') -plt.xlim(0, 0.2) -plt.xticks([]) -plt.yticks([]) +left_inset_ax = fig.add_axes([.2, .6, .2, .2], facecolor='k') +left_inset_ax.plot(t[:len(r)], r) +left_inset_ax.set_title('Impulse response') +left_inset_ax.set_xlim(0, 0.2) +left_inset_ax.set_xticks([]) +left_inset_ax.set_yticks([]) plt.show()