8000 Add axes3d.automargin rcparam to retain original margin behavior · matplotlib/matplotlib@9ed0c5b · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ed0c5b

Browse files
Add axes3d.automargin rcparam to retain original margin behavior
1 parent b75b0e6 commit 9ed0c5b

File tree

7 files changed

+82
-29
lines changed

7 files changed

+82
-29
lines changed

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,9 @@
424424
#axes.autolimit_mode: data # If "data", use axes.xmargin and axes.ymargin as is.
425425
# If "round_numbers", after application of margins, axis
426426
# limits are further expanded to the nearest "round" number.
427-
#polaraxes.grid: True # display grid on polar axes
428-
#axes3d.grid: True # display grid on 3D axes
427+
#polaraxes.grid: True # display grid on polar axes
428+
#axes3d.grid: True # display grid on 3D axes
429+
#axes3d.automargin: False # automatically add margin when setting 3D axis limits
429430

430431
#axes3d.xaxis.panecolor: (0.95, 0.95, 0.95, 0.5) # background pane on 3D axes
431432
#axes3d.yaxis.panecolor: (0.90, 0.90, 0.90, 0.5) # background pane on 3D axes

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ axes.spines.left : True
223223
axes.spines.right : True
224224
axes.spines.top : True
225225
polaraxes.grid : True # display grid on polar axes
226-
axes3d.grid : True # display grid on 3d axes
226+
axes3d.grid : True # display grid on 3D axes
227+
axes3d.automargin : False # automatically add margin when setting 3D axis limits
227228

228229
date.autoformatter.year : %Y
229230
date.autoformatter.month : %b %Y

lib/matplotlib/rcsetup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,10 @@ def _convert_validator_spec(key, conv):
10151015
"axes.ymargin": _range_validators["0 <= x <= 1"], # margin added to yaxis
10161016
'axes.zmargin': _range_validators["0 <= x <= 1"], # margin added to zaxis
10171017

1018-
"polaraxes.grid": validate_bool, # display polar grid or not
1019-
"axes3d.grid": validate_bool, # display 3d grid
1018+
"polaraxes.grid": validate_bool, # display polar grid or not
1019+
"axes3d.grid": validate_bool, # display 3d grid
1020+
"axes3d.automargin": validate_bool, # automatically add margin when
1021+
# setting 3D axis limits
10201022

10211023
"axes3d.xaxis.panecolor": validate_color, # 3d background pane
10221024
"axes3d.yaxis.panecolor": validate_color, # 3d background pane

lib/matplotlib/ticker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,8 @@ def _raw_ticks(self, vmin, vmax):
20702070
steps = steps[igood]
20712071

20722072
raw_step = ((_vmax - _vmin) / nbins)
2073+
if self.axis.axes.name == '3d':
2074+
raw_step = raw_step * 24/25 # needed to match mpl3.7 appearance
20732075
large_steps = steps >= raw_step
20742076
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
20752077
# Classic round_numbers mode may require a larger step.

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ def __init__(
158158
self.M = None
159159

160160
# Initialize margins, which calls autoscale_view
161-
margin = 1/48 # default value
162-
self.margins(x=margin, y=margin, z=margin)
161+
self.margin = 1/48 # default value
162+
self.margins(x=self.margin, y=self.margin, z=self.margin)
163163

164164
# func used to format z -- fall back on major formatters
165165
self.fmt_zdata = None
@@ -341,7 +341,8 @@ def set_aspect(self, aspect, adjustable=None, anchor=None, share=False):
341341
self.set_ylim3d,
342342
self.set_zlim3d)):
343343
if i in ax_indices:
344-
set_lim(mean[i] - deltas[i]/2., mean[i] + deltas[i]/2.)
344+
set_lim(mean[i] - deltas[i]/2., mean[i] + deltas[i]/2.,
345+
auto=None)
345346
else: # 'box'
346347
# Change the box aspect such that the ratio of the length of
347348
# the unmodified axis to the length of the diagonal
@@ -409,8 +410,8 @@ def set_box_aspect(self, aspect, *, zoom=1):
409410
else:
410411
aspect = np.asarray(aspect, dtype=float)
411412
_api.check_shape((3,), aspect=aspect)
412-
# default scale tuned to match the mpl32 appearance.
413-
aspect *= 1.8294640721620434 * zoom / np.linalg.norm(aspect)
413+
# default scale tuned to match the mpl3.2 appearance.
414+
aspect *= 1.905691741835462 * zoom / np.linalg.norm(aspect)
414415

415416
self._box_aspect = aspect
416417
self.stale = True
@@ -681,28 +682,73 @@ def get_w_lims(self):
681682
minz, maxz = self.get_zlim3d()
682683
return minx, maxx, miny, maxy, minz, maxz
683684

684-
# set_xlim, set_ylim are directly inherited from base Axes.
685+
def _set_lim3d(self, axis, lower=None, upper=None, *, emit=True,
686+
auto=False, margin=None, axmin=None, axmax=None):
687+
"""
688+
Set 3D axis limits.
689+
690+
See `.Axes.set_ylim` for full documentation
691+
"""
692+
if upper is None:
693+
if np.iterable(lower):
694+
lower, upper = lower
695+
else:
696+
raise ValueError("Must provide an upper bound.")
697+
if lower is None:
698+
raise ValueError("Must provide a lower bound.")
699+
if axmin is not None:
700+
if lower is not None:
701+
raise TypeError("Cannot pass both 'lower' and 'min'")
702+
lower = axmin
703+
if axmax is not None:
704+
if upper is not None:
705+
raise TypeError("Cannot pass both 'upper' and 'max'")
706+
upper = axmax
707+
if margin is None:
708+
if mpl.rcParams['axes3d.automargin']:
709+
margin = self.margin
710+
else:
711+
margin = 0
712+
delta = (upper - lower) * margin
713+
lower = lower - delta
714+
upper = upper + delta
715+
return axis._set_lim(lower, upper, emit=emit, auto=auto)
716+
717+
@_api.make_keyword_only("3.8", "emit")
718+
def set_xlim(self, left=None, right=None, *, emit=True, auto=False,
719+
margin=None, xmin=None, xmax=None):
720+
"""
721+
Set 3D x limits.
722+
723+
See `.Axes.set_xlim` for full documentation
724+
"""
725+
return self._set_lim3d(self.xaxis, left, right, emit=emit, auto=auto,
726+
margin=margin, axmin=xmin, axmax=xmax)
727+
728+
@_api.make_keyword_only("3.8", "emit")
729+
def set_ylim(self, bottom=None, top=None, *, emit=True, auto=False,
730+
margin=None, ymin=None, ymax=None):
731+
"""
732+
Set 3D y limits.
733+
734+
See `.Axes.set_ylim` for full documentation
735+
"""
736+
return self._set_lim3d(self.yaxis, bottom, top, emit=emit, auto=auto,
737+
margin=margin, axmin=ymin, axmax=ymax)
738+
739+
@_api.make_keyword_only("3.6", "emit")
685740
def set_zlim(self, bottom=None, top=None, *, emit=True, auto=False,
686-
zmin=None, zmax=None):
741+
margin=None, zmin=None, zmax=None):
687742
"""
688743
Set 3D z limits.
689744
690745
See `.Axes.set_ylim` for full documentation
691746
"""
692-
if top is None and np.iterable(bottom):
693-
bottom, top = bottom
694-
if zmin is not None:
695-
if bottom is not None:
696-
raise TypeError("Cannot pass both 'bottom' and 'zmin'")
697-
bottom = zmin
698-
if zmax is not None:
699-
if top is not None:
700-
raise TypeError("Cannot pass both 'top' and 'zmax'")
701-
top = zmax
702-
return self.zaxis._set_lim(bottom, top, emit=emit, auto=auto)
703-
704-
set_xlim3d = maxes.Axes.set_xlim
705-
set_ylim3d = maxes.Axes.set_ylim
747+
return self._set_lim3d(self.zaxis, bottom, top, emit=emit, auto=auto,
748+
margin=margin, axmin=zmin, axmax=zmax)
749+
750+
set_xlim3d = set_xlim
751+
set_ylim3d = set_ylim
706752
set_zlim3d = set_zlim
707753

708754
def get_xlim(self):

lib/mpl_toolkits/mplot3d/axis3d.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def draw(self, renderer):
324324

325325
mins, maxs, tc, highs = self._get_coord_info()
326326
centers = 0.5 * (maxs + mins)
327-
deltas = (maxs - mins) / 12 # label offsets
327+
deltas = (maxs - mins) * 1/12 * 23/24 # keep mpl3.7 appearance
328328

329329
minmax = np.where(highs, maxs, mins)
330330
maxmin = np.where(~highs, maxs, mins)
@@ -479,7 +479,6 @@ def draw_grid(self, renderer):
479479
if not self.axes._draw_grid:
480480
return
481481

482-
self.label._transform = self.axes.transData
483482
renderer.open_group("grid3d", gid=self.get_gid())
484483

485484
ticks = self._update_ticks()

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ def test_add_collection3d_zs_array():
927927

928928
assert line is not None
929929

930+
plt.rcParams['axes3d.automargin'] = True
930931
ax.set_xlim(-5, 5)
931932
ax.set_ylim(-4, 6)
932933
ax.set_zlim(-2, 2)
@@ -953,6 +954,7 @@ def test_add_collection3d_zs_scalar():
953954

954955
assert line is not None
955956

957+
plt.rcParams['axes3d.automargin'] = True
956958
ax.set_xlim(-5, 5)
957959
ax.set_ylim(-4, 6)
958960
ax.set_zlim(0, 2)
@@ -977,7 +979,7 @@ def test_axes3d_labelpad():
977979

978980
# Tick labels also respect tick.pad (also from rcParams)
979981
for i, tick in enumerate(ax.yaxis.get_major_ticks()):
980-
tick.set_pad(tick.get_pad() - i * 5)
982+
tick.set_pad(tick.get_pad() + 5 - i * 5)
981983

982984

983985
@mpl3d_image_comparison(['axes3d_cla.png'], remove_text=False)

0 commit comments

Comments
 (0)
0