matplotlib.pyplot.margins#
- matplotlib.pyplot.margins(*margins,x=None,y=None,tight=True)[source]#
Set or retrieve margins around the data for autoscaling axis limits.
This allows to configure the padding around the data without having toset explicit limits using
set_xlim
/set_ylim
.Autoscaling determines the axis limits by addingmargin times thedata interval as padding around the data. See the following illustration:
All input parameters must be floats greater than -0.5. Passing bothpositional and keyword arguments is invalid and will raise a TypeError.If no arguments (positional or otherwise) are provided, the currentmargins will remain unchanged and simply be returned.
The default margins are
rcParams["axes.xmargin"]
(default:0.05
) andrcParams["axes.ymargin"]
(default:0.05
).- Parameters:
- *marginsfloat, optional
If a single positional argument is provided, it specifiesboth margins of the x-axis and y-axis limits. If twopositional arguments are provided, they will be interpretedasxmargin,ymargin. If setting the margin on a singleaxis is desired, use the keyword arguments described below.
- x, yfloat, optional
Specific margin values for the x-axis and y-axis,respectively. These cannot be used with positionalarguments, but can be used individually to alter on e.g.,only the y-axis.
- tightbool or None, default: True
Thetight parameter is passed to
autoscale_view
,which is executed after a margin is changed; the defaulthere isTrue, on the assumption that when margins arespecified, no additional padding to match tick marks isusually desired. Settingtight toNone preservesthe previous setting.
- Returns:
- xmargin, ymarginfloat
See also
Notes
Note
This is thepyplot wrapper for
axes.Axes.margins
.If a previously used Axes method such as
pcolor()
has setuse_sticky_edges
toTrue
, only the limits not set bythe "sticky artists" will be modified. To force allmargins to be set, setuse_sticky_edges
toFalse
before callingmargins()
.
Examples usingmatplotlib.pyplot.margins
#

Controlling view limits using margins and sticky_edges