Skip to content

Commit cd730bf

Browse files
committed
Scale margins to data, plus padding, by default
Fix matplotlib#4891
1 parent 777c7a8 commit cd730bf

5 files changed

Lines changed: 27 additions & 13 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
@@ -987,7 +987,7 @@ def cla(self):
987987
self._autoscaleYon = True
988988
self._xmargin = rcParams['axes.xmargin']
989989
self._ymargin = rcParams['axes.ymargin']
990-
self._tight = False
990+
self._tight = None
991991
self._update_transScale() # needed?
992992

993993
self._get_lines = _process_plot_var_args(self)
@@ -2133,10 +2133,11 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
21332133
autoscale_view.
21342134
"""
21352135
if tight is None:
2136-
# if image data only just use the datalim
2137-
_tight = self._tight or (len(self.images) > 0 and
2138-
len(self.lines) == 0 and
2139-
len(self.patches) == 0)
2136+
if self._tight is None:
2137+
_tight = rcParams['axes.autolimit_mode'] == 'data'
2138+
else:
2139+
# if image data only just use the datalim
2140+
_tight = self._tight
21402141
else:
21412142
_tight = self._tight = bool(tight)
21422143

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

Lines changed: 1 addition & 0 deletions
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

‎lib/matplotlib/rcsetup.py‎

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

977982
'polaraxes.grid': [True, validate_bool], # display polar grid or
978983
# not

‎matplotlibrc.template‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ backend : %(backend)s
317317
# as list of string colorspecs:
318318
# single letter, long name, or
319319
# web-style hex
320-
#axes.xmargin : 0 # x margin. See `axes.Axes.margins`
321-
#axes.ymargin : 0 # y margin See `axes.Axes.margins`
320+
#axes.autolimit_mode : data # scale axes limits to the data.
321+
# Use "round_numbers" to round to nearest round numbers
322+
#axes.xmargin : .05 # x margin. See `axes.Axes.margins`
323+
#axes.ymargin : .05 # y margin See `axes.Axes.margins`
322324

323325
#polaraxes.grid : True # display grid on polar axes
324326
#axes3d.grid : True # display grid on 3d axes

0 commit comments

Comments
 (0)