From 7f320b148ec9aecb1c3ef91645b3b57e67c92264 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Tue, 30 Jul 2019 16:33:55 -0700 Subject: [PATCH 1/2] 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. --- .../subplots_axes_and_figures/axes_demo.py | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/subplots_axes_and_figures/axes_demo.py b/examples/subplots_axes_and_figures/axes_demo.py index 5b07f2f2d820..3a08889902b1 100644 --- a/examples/subplots_axes_and_figures/axes_demo.py +++ b/examples/subplots_axes_and_figures/axes_demo.py @@ -3,7 +3,7 @@ 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. """ import matplotlib.pyplot as plt import numpy as np @@ -19,27 +19,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() From d99740c568315b422d68166c668a80c893b378f6 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Wed, 31 Jul 2019 12:07:08 -0700 Subject: [PATCH 2/2] Link to relevant examples --- examples/subplots_axes_and_figures/axes_demo.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/subplots_axes_and_figures/axes_demo.py b/examples/subplots_axes_and_figures/axes_demo.py index 3a08889902b1..972567420434 100644 --- a/examples/subplots_axes_and_figures/axes_demo.py +++ b/examples/subplots_axes_and_figures/axes_demo.py @@ -4,6 +4,13 @@ ========= 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