From e3280c67845040bc1926d13d1160783a3d6ab74c Mon Sep 17 00:00:00 2001 From: Atharva Khare Date: Tue, 17 Oct 2017 10:14:00 +0530 Subject: [PATCH 1/3] Modified restrictions on the margins methods Axes.set_xmargin and Axes.set_ymargin methods now take values greater than -0.5. Margins greater than 0 zoom out and margins between -0.5 and 0 zoom in. Modified `lib/matplotlib/test/test_axes.py::test_margins` to reflect the changed method. Added an example for `Axes.margins` at `example/axes_grid1/axes_margins.py`. --- examples/axes_grid1/axes_margins.py | 34 +++++++++++++++++++++++++++++ lib/matplotlib/axes/_base.py | 12 +++++----- lib/matplotlib/tests/test_axes.py | 24 ++++++++++++++++---- 3 files changed, 60 insertions(+), 10 deletions(-) create mode 100755 examples/axes_grid1/axes_margins.py diff --git a/examples/axes_grid1/axes_margins.py b/examples/axes_grid1/axes_margins.py new file mode 100755 index 000000000000..bfc5a9f7958d --- /dev/null +++ b/examples/axes_grid1/axes_margins.py @@ -0,0 +1,34 @@ +""" +============ +Margins demo +============ + +The following example shows an alternative method for scaling +by using Axes.margins instead of Axes.set_xlim and Axes.set_ylim. + +""" +import numpy as np +import matplotlib.pyplot as plt + + +def f(t): + return np.exp(-t) * np.cos(2*np.pi*t) + + +t1 = np.arange(0.0, 3.0, 0.01) + +ax1 = plt.subplot(212) +ax1.margins(0.05) # Default margin is 0.05, value 0 means fit +ax1.plot(t1, f(t1), 'k') + +ax2 = plt.subplot(221) +ax2.margins(2, 2) # Values >0.0 zoom out +ax2.plot(t1, f(t1), 'r') +ax2.set_title('Zoomed out') + +ax3 = plt.subplot(222) +ax3.margins(x=0, y=-0.25) # Values in (-0.5, 0.0) zooms in to center +ax3.plot(t1, f(t1), 'g') +ax3.set_title('Zoomed in') + +plt.show() diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 00cc9ee7aa4a..b70700fc3785 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2075,10 +2075,10 @@ def set_xmargin(self, m): *m* times the data interval will be added to each end of that interval before it is used in autoscaling. - accepts: float in range 0 to 1 + accepts: float greater than -0.5 """ - if m < 0 or m > 1: - raise ValueError("margin must be in range 0 to 1") + if m <= -0.5: + raise ValueError("margin must be greater than -0.5") self._xmargin = m self.stale = True @@ -2089,10 +2089,10 @@ def set_ymargin(self, m): *m* times the data interval will be added to each end of that interval before it is used in autoscaling. - accepts: float in range 0 to 1 + accepts: float greater than -0.5 """ - if m < 0 or m > 1: - raise ValueError("margin must be in range 0 to 1") + if m <= -0.5: + raise ValueError("margin must be greater than -0.5") self._ymargin = m self.stale = True diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 6a2ec213329e..891d9a687fe5 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4585,21 +4585,37 @@ def test_o_marker_path_snap(): def test_margins(): # test all ways margins can be called data = [1, 10] + xmin = 0.0 + xmax = len(data) - 1.0 + ymin = min(data) + ymax = max(data) fig1, ax1 = plt.subplots(1, 1) ax1.plot(data) ax1.margins(1) assert ax1.margins() == (1, 1) + assert ax1.get_xlim() == (xmin - (xmax - xmin) * 1, + xmax + (xmax - xmin) * 1) + assert ax1.get_ylim() == (ymin - (ymax - ymin) * 1, + ymax + (ymax - ymin) * 1) fig2, ax2 = plt.subplots(1, 1) ax2.plot(data) - ax2.margins(1, 0.5) - assert ax2.margins() == (1, 0.5) + ax2.margins(0.5, 2) + assert ax2.margins() == (0.5, 2) + assert ax2.get_xlim() == (xmin - (xmax - xmin) * 0.5, + xmax + (xmax - xmin) * 0.5) + assert ax2.get_ylim() == (ymin - (ymax - ymin) * 2, + ymax + (ymax - ymin) * 2) fig3, ax3 = plt.subplots(1, 1) ax3.plot(data) - ax3.margins(x=1, y=0.5) - assert ax3.margins() == (1, 0.5) + ax3.margins(x=-0.2, y=0.5) + assert ax3.margins() == (-0.2, 0.5) + assert ax3.get_xlim() == (xmin - (xmax - xmin) * -0.2, + xmax + (xmax - xmin) * -0.2) + assert ax3.get_ylim() == (ymin - (ymax - ymin) * 0.5, + ymax + (ymax - ymin) * 0.5) def test_length_one_hist(): From 0fb8573797f808749cc1228a1796618a736cec87 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Wed, 18 Oct 2017 19:36:16 -0700 Subject: [PATCH 2/3] DOC axes_margins.py example --- examples/axes_grid1/axes_margins.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/axes_grid1/axes_margins.py b/examples/axes_grid1/axes_margins.py index bfc5a9f7958d..ec000d661cbe 100755 --- a/examples/axes_grid1/axes_margins.py +++ b/examples/axes_grid1/axes_margins.py @@ -1,11 +1,10 @@ """ -============ -Margins demo -============ - -The following example shows an alternative method for scaling -by using Axes.margins instead of Axes.set_xlim and Axes.set_ylim. +===================================== +Zooming in and out using Axes.margins +===================================== +This example shows how to zoom in and out of a plot using Axes.margins +instead of Axes.set_xlim and Axes.set_ylim. """ import numpy as np import matplotlib.pyplot as plt From ae75bcb52c1f269e57a23f59e560ceaa9e344935 Mon Sep 17 00:00:00 2001 From: Nelle Varoquaux Date: Wed, 18 Oct 2017 19:37:03 -0700 Subject: [PATCH 3/3] MAINT moved axes_margins.py to api_examples --- examples/{axes_grid1 => api_examples}/axes_margins.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{axes_grid1 => api_examples}/axes_margins.py (100%) diff --git a/examples/axes_grid1/axes_margins.py b/examples/api_examples/axes_margins.py similarity index 100% rename from examples/axes_grid1/axes_margins.py rename to examples/api_examples/axes_margins.py