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

Skip to content

Commit e3280c6

Browse files
committed
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`.
1 parent 7cca455 commit e3280c6

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

examples/axes_grid1/axes_margins.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
============
3+
Margins demo
4+
============
5+
6+
The following example shows an alternative method for scaling
7+
by using Axes.margins instead of Axes.set_xlim and Axes.set_ylim.
8+
9+
"""
10+
import numpy as np
11+
import matplotlib.pyplot as plt
12+
13+
14+
def f(t):
15+
return np.exp(-t) * np.cos(2*np.pi*t)
16+
17+
18+
t1 = np.arange(0.0, 3.0, 0.01)
19+
20+
ax1 = plt.subplot(212)
21+
ax1.margins(0.05) # Default margin is 0.05, value 0 means fit
22+
ax1.plot(t1, f(t1), 'k')
23+
24+
ax2 = plt.subplot(221)
25+
ax2.margins(2, 2) # Values >0.0 zoom out
26+
ax2.plot(t1, f(t1), 'r')
27+
ax2.set_title('Zoomed out')
28+
29+
ax3 = plt.subplot(222)
30+
ax3.margins(x=0, y=-0.25) # Values in (-0.5, 0.0) zooms in to center
31+
ax3.plot(t1, f(t1), 'g')
32+
ax3.set_title('Zoomed in')
33+
34+
plt.show()

lib/matplotlib/axes/_base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,10 +2075,10 @@ def set_xmargin(self, m):
20752075
*m* times the data interval will be added to each
20762076
end of that interval before it is used in autoscaling.
20772077
2078-
accepts: float in range 0 to 1
2078+
accepts: float greater than -0.5
20792079
"""
2080-
if m < 0 or m > 1:
2081-
raise ValueError("margin must be in range 0 to 1")
2080+
if m <= -0.5:
2081+
raise ValueError("margin must be greater than -0.5")
20822082
self._xmargin = m
20832083
self.stale = True
20842084

@@ -2089,10 +2089,10 @@ def set_ymargin(self, m):
20892089
*m* times the data interval will be added to each
20902090
end of that interval before it is used in autoscaling.
20912091
2092-
accepts: float in range 0 to 1
2092+
accepts: float greater than -0.5
20932093
"""
2094-
if m < 0 or m > 1:
2095-
raise ValueError("margin must be in range 0 to 1")
2094+
if m <= -0.5:
2095+
raise ValueError("margin must be greater than -0.5")
20962096
self._ymargin = m
20972097
self.stale = True
20982098

lib/matplotlib/tests/test_axes.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4585,21 +4585,37 @@ def test_o_marker_path_snap():
45854585
def test_margins():
45864586
# test all ways margins can be called
45874587
data = [1, 10]
4588+
xmin = 0.0
4589+
xmax = len(data) - 1.0
4590+
ymin = min(data)
4591+
ymax = max(data)
45884592

45894593
fig1, ax1 = plt.subplots(1, 1)
45904594
ax1.plot(data)
45914595
ax1.margins(1)
45924596
assert ax1.margins() == (1, 1)
4597+
assert ax1.get_xlim() == (xmin - (xmax - xmin) * 1,
4598+
xmax + (xmax - xmin) * 1)
4599+
assert ax1.get_ylim() == (ymin - (ymax - ymin) * 1,
4600+
ymax + (ymax - ymin) * 1)
45934601

45944602
fig2, ax2 = plt.subplots(1, 1)
45954603
ax2.plot(data)
4596-
ax2.margins(1, 0.5)
4597-
assert ax2.margins() == (1, 0.5)
4604+
ax2.margins(0.5, 2)
4605+
assert ax2.margins() == (0.5, 2)
4606+
assert ax2.get_xlim() == (xmin - (xmax - xmin) * 0.5,
4607+
xmax + (xmax - xmin) * 0.5)
4608+
assert ax2.get_ylim() == (ymin - (ymax - ymin) * 2,
4609+
ymax + (ymax - ymin) * 2)
45984610

45994611
fig3, ax3 = plt.subplots(1, 1)
46004612
ax3.plot(data)
4601-
ax3.margins(x=1, y=0.5)
4602-
assert ax3.margins() == (1, 0.5)
4613+
ax3.margins(x=-0.2, y=0.5)
4614+
assert ax3.margins() == (-0.2, 0.5)
4615+
assert ax3.get_xlim() == (xmin - (xmax - xmin) * -0.2,
4616+
xmax + (xmax - xmin) * -0.2)
4617+
assert ax3.get_ylim() == (ymin - (ymax - ymin) * 0.5,
4618+
ymax + (ymax - ymin) * 0.5)
46034619

46044620

46054621
def test_length_one_hist():

0 commit comments

Comments
 (0)