Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5eb185f

Browse filesBrowse files
authored
Merge pull request #9459 from AtharvaKhare/flexible-axis-margins
Modified restrictions on `margins` method
2 parents 1a50874 + ae75bcb commit 5eb185f
Copy full SHA for 5eb185f

File tree

Expand file treeCollapse file tree

3 files changed

+59
-10
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+59
-10
lines changed

‎examples/api_examples/axes_margins.py

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

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+6-6Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+20-4Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4604,21 +4604,37 @@ def test_o_marker_path_snap():
46044604
def test_margins():
46054605
# test all ways margins can be called
46064606
data = [1, 10]
4607+
xmin = 0.0
4608+
xmax = len(data) - 1.0
4609+
ymin = min(data)
4610+
ymax = max(data)
46074611

46084612
fig1, ax1 = plt.subplots(1, 1)
46094613
ax1.plot(data)
46104614
ax1.margins(1)
46114615
assert ax1.margins() == (1, 1)
4616+
assert ax1.get_xlim() == (xmin - (xmax - xmin) * 1,
4617+
xmax + (xmax - xmin) * 1)
4618+
assert ax1.get_ylim() == (ymin - (ymax - ymin) * 1,
4619+
ymax + (ymax - ymin) * 1)
46124620

46134621
fig2, ax2 = plt.subplots(1, 1)
46144622
ax2.plot(data)
4615-
ax2.margins(1, 0.5)
4616-
assert ax2.margins() == (1, 0.5)
4623+
ax2.margins(0.5, 2)
4624+
assert ax2.margins() == (0.5, 2)
4625+
assert ax2.get_xlim() == (xmin - (xmax - xmin) * 0.5,
4626+
xmax + (xmax - xmin) * 0.5)
4627+
assert ax2.get_ylim() == (ymin - (ymax - ymin) * 2,
4628+
ymax + (ymax - ymin) * 2)
46174629

46184630
fig3, ax3 = plt.subplots(1, 1)
46194631
ax3.plot(data)
4620-
ax3.margins(x=1, y=0.5)
4621-
assert ax3.margins() == (1, 0.5)
4632+
ax3.margins(x=-0.2, y=0.5)
4633+
assert ax3.margins() == (-0.2, 0.5)
4634+
assert ax3.get_xlim() == (xmin - (xmax - xmin) * -0.2,
4635+
xmax + (xmax - xmin) * -0.2)
4636+
assert ax3.get_ylim() == (ymin - (ymax - ymin) * 0.5,
4637+
ymax + (ymax - ymin) * 0.5)
46224638

46234639

46244640
def test_length_one_hist():

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.