Skip to content

Commit ceae4fc

Browse files
committed
Add margins() method to Axes, function to pyplot
svn path=/trunk/matplotlib/; revision=8249
1 parent fd6cb89 commit ceae4fc

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2010-04-20 Added margins() Axes method and pyplot function. - EF
2+
13
2010-04-18 update the axes_grid documentation. -JJL
24

35
2010-04-18 Control MaxNLocator parameters after instantiation,

boilerplate.py

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def %(func)s(%(argspec)s):
103103
'annotate',
104104
'ticklabel_format',
105105
'locator_params',
106+
'margins',
106107
)
107108

108109
cmappable = {

lib/matplotlib/axes.py

+90-1
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,8 @@ def cla(self):
836836

837837
self._autoscaleXon = True
838838
self._autoscaleYon = True
839+
self._xmargin = 0
840+
self._ymargin = 0
839841
self._update_transScale() # needed?
840842

841843
self._get_lines = _process_plot_var_args(self)
@@ -1605,6 +1607,83 @@ def set_autoscaley_on(self, b):
16051607
"""
16061608
self._autoscaleYon = b
16071609

1610+
def set_xmargin(self, m):
1611+
"""
1612+
Set padding of X data limits prior to autoscaling.
1613+
1614+
*m* times the data interval will be added to each
1615+
end of that interval before it is used in autoscaling.
1616+
1617+
accepts: float in range 0 to 1
1618+
"""
1619+
if m < 0 or m > 1:
1620+
raise ValueError("margin must be in range 0 to 1")
1621+
self._xmargin = m
1622+
1623+
def set_ymargin(self, m):
1624+
"""
1625+
Set padding of Y data limits prior to autoscaling.
1626+
1627+
*m* times the data interval will be added to each
1628+
end of that interval before it is used in autoscaling.
1629+
1630+
accepts: float in range 0 to 1
1631+
"""
1632+
if m < 0 or m > 1:
1633+
raise ValueError("margin must be in range 0 to 1")
1634+
self._ymargin = m
1635+
1636+
1637+
def margins(self, *args, **kw):
1638+
"""
1639+
Convenience method to set or retrieve autoscaling margins.
1640+
1641+
signatures::
1642+
1643+
margins()
1644+
1645+
returns xmargin, ymargin
1646+
1647+
::
1648+
1649+
margins(margin, tight=True)
1650+
1651+
margins(xmargin, ymargin, tight=True)
1652+
1653+
margins(x=xmargin, y=ymargin, tight=True)
1654+
1655+
All three forms above set the xmargin and ymargin parameters.
1656+
All keyword parameters are optional. A single argument
1657+
specifies both xmargin and ymargin. The *tight* parameter
1658+
is passed to :meth:`autoscale_view`, which is executed after
1659+
a margin is changed.
1660+
1661+
Specifying any margin changes only the autoscaling; for example,
1662+
if *xmargin* is not zero, then *xmargin* times the X data
1663+
interval will be added to each end of that interval before
1664+
it is used in autoscaling.
1665+
1666+
"""
1667+
if not args and not kw:
1668+
return self._ymargin, self._ymargin
1669+
1670+
tight = kw.pop('tight', False)
1671+
mx = kw.pop('x', None)
1672+
my = kw.pop('y', None)
1673+
if len(args) == 1:
1674+
mx = my = args[0]
1675+
elif len(args) == 2:
1676+
mx, my = args
1677+
else:
1678+
raise ValueError("more than two arguments were supplied")
1679+
if mx is not None:
1680+
self.set_xmargin(mx)
1681+
if my is not None:
1682+
self.set_ymargin(my)
1683+
1684+
self.autoscale_view(tight=tight, scalex=bool(mx), scaley=bool(my))
1685+
1686+
16081687
def set_rasterization_zorder(self, z):
16091688
"""
16101689
Set zorder value below which artists will be rasterized
@@ -1631,11 +1710,21 @@ def autoscale_view(self, tight=False, scalex=True, scaley=True):
16311710
dl = [ax.dataLim for ax in xshared]
16321711
bb = mtransforms.BboxBase.union(dl)
16331712
x0, x1 = bb.intervalx
1713+
if self._xmargin > 0:
1714+
delta = (x1 - x0) * self._xmargin
1715+
x0 -= delta
1716+
x1 += delta
1717+
16341718
if scaley and self._autoscaleYon:
16351719
yshared = self._shared_y_axes.get_siblings(self)
16361720
dl = [ax.dataLim for ax in yshared]
16371721
bb = mtransforms.BboxBase.union(dl)
16381722
y0, y1 = bb.intervaly
1723+
if self._ymargin > 0:
1724+
delta = (y1 - y0) * self._ymargin
1725+
y0 -= delta
1726+
y1 += delta
1727+
16391728
if (tight or (len(self.images)>0 and
16401729
len(self.lines)==0 and
16411730
len(self.patches)==0)):
@@ -1958,7 +2047,7 @@ def locator_params(self, axis='both', tight=False, **kwargs):
19582047
of ticks and use tight bounds when plotting small
19592048
subplots, for example::
19602049
1961-
ax.set_locator_params(tight=True, nbins=4)
2050+
ax.locator_params(tight=True, nbins=4)
19622051
19632052
Because the locator is involved in autoscaling,
19642053
:meth:`autoscale_view` is called automatically after

lib/matplotlib/pyplot.py

+8
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,14 @@ def locator_params(axis='both', tight=False, **kwargs):
25432543
draw_if_interactive()
25442544
return ret
25452545

2546+
# This function was autogenerated by boilerplate.py. Do not edit as
2547+
# changes will be lost
2548+
@docstring.copy_dedent(Axes.margins)
2549+
def margins(*args, **kw):
2550+
ret = gca().margins(*args, **kw)
2551+
draw_if_interactive()
2552+
return ret
2553+
25462554
# This function was autogenerated by boilerplate.py. Do not edit as
25472555
# changes will be lost
25482556
def autumn():

0 commit comments

Comments
 (0)