Skip to content

Commit 3847f52

Browse files
committed
Scale margins to data, plus padding, by default
Fix matplotlib#4891
1 parent 60f1b71 commit 3847f52

5 files changed

Lines changed: 28 additions & 14 deletions

File tree

‎doc/users/whats_new/style_changes.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ These changes include:
1111

1212
- The default figure background color is now white.
1313

14+
- The limits are scaled to exactly the dimensions of the data, plus 5%
15+
padding. The old behavior was to scale to the nearest "round"
16+
numbers. To use the old behavior, set the ``rcParam``
17+
``axes.autolimit_mode`` to ``round_numbers``.
18+
1419
- Grid lines are light grey solid 1pt lines. They are no longer dashed by
1520
default.
1621

‎lib/matplotlib/axes/_base.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ def cla(self):
982982
self._autoscaleYon = True
983983
self._xmargin = rcParams['axes.xmargin']
984984
self._ymargin = rcParams['axes.ymargin']
985-
self._tight = False
985+
self._tight = None
986986
self._update_transScale() # needed?
987987

988988
self._get_lines = _process_plot_var_args(self)
@@ -2120,10 +2120,11 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
21202120
autoscale_view.
21212121
"""
21222122
if tight is None:
2123-
# if image data only just use the datalim
2124-
_tight = self._tight or (len(self.images) > 0 and
2125-
len(self.lines) == 0 and
2126-
len(self.patches) == 0)
2123+
if self._tight is None:
2124+
_tight = rcParams['axes.autolimit_mode'] == 'data'
2125+
else:
2126+
# if image data only just use the datalim
2127+
_tight = self._tight
21272128
else:
21282129
_tight = self._tight = bool(tight)
21292130

‎lib/matplotlib/mpl-data/stylelib/classic.mplstyle‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ axes.prop_cycle : cycler('color', 'bgrcmyk')
203203
# as list of string colorspecs:
204204
# single letter, long name, or
205205
# web-style hex
206+
axes.autolimit_mode : round_numbers
206207
axes.xmargin : 0 # x margin. See `axes.Axes.margins`
207208
axes.ymargin : 0 # y margin See `axes.Axes.margins`
208209
axes.spines.bottom : True
@@ -418,7 +419,7 @@ pdf.use14corefonts : False
418419
pgf.debug : False
419420
pgf.texsystem : xelatex
420421
pgf.rcfonts : True
421-
pgf.preamble :
422+
pgf.preamble :
422423

423424
# svg backend params
424425
svg.image_inline : True # write raster image data directly into the svg file

‎lib/matplotlib/rcsetup.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -964,12 +964,17 @@ def validate_cycler(s):
964964
# to create the object.
965965
'axes.prop_cycle': [ccycler('color', 'bgrcmyk'),
966966
validate_cycler],
967-
'axes.xmargin': [0, ValidateInterval(0, 1,
968-
closedmin=True,
969-
closedmax=True)], # margin added to xaxis
970-
'axes.ymargin': [0, ValidateInterval(0, 1,
971-
closedmin=True,
972-
closedmax=True)],# margin added to yaxis
967+
# If 'data', axes limits are set close to the data.
968+
# If 'round_numbers' axes limits are set to the nearest round numbers.
969+
'axes.autolimit_mode': [
970+
'data',
971+
ValidateInStrings('autolimit_mode', ['data', 'round_numbers'])],
972+
'axes.xmargin': [0.05, ValidateInterval(0, 1,
973+
closedmin=True,
974+
closedmax=True)], # margin added to xaxis
975+
'axes.ymargin': [0.05, ValidateInterval(0, 1,
976+
closedmin=True,
977+
closedmax=True)],# margin added to yaxis
973978

974979
'polaraxes.grid': [True, validate_bool], # display polar grid or
975980
# not

‎matplotlibrc.template‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,10 @@ backend : %(backend)s
273273
# as list of string colorspecs:
274274
# single letter, long name, or
275275
# web-style hex
276-
#axes.xmargin : 0 # x margin. See `axes.Axes.margins`
277-
#axes.ymargin : 0 # y margin See `axes.Axes.margins`
276+
#axes.autolimit_mode : data # scale axes limits to the data.
277+
# Use "round_numbers" to round to nearest round numbers
278+
#axes.xmargin : .05 # x margin. See `axes.Axes.margins`
279+
#axes.ymargin : .05 # y margin See `axes.Axes.margins`
278280

279281
#polaraxes.grid : True # display grid on polar axes
280282
#axes3d.grid : True # display grid on 3d axes

0 commit comments

Comments
 (0)