Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b55d12c

Browse files
Code review updates
1 parent 43f7d59 commit b55d12c

File tree

5 files changed

+91
-20
lines changed

5 files changed

+91
-20
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Setting 3D axis limits now set the limits exactly
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Previously, setting the limits of a 3D axis would always add a small margin to
5+
the limits. Limits are now set exactly by default. The newly introduced rcparam
6+
``axes3d.automargin`` can be used to revert to the old behavior.
7+
8+
.. plot::
9+
:include-source: true
10+
:alt: Example of the new behavior of 3D axis limits, and how setting the rcparam reverts to the old behavior.
11+
12+
import matplotlib.pyplot as plt
13+
fig, axs = plt.subplots(1, 2, subplot_kw={'projection': '3d'})
14+
plt.rcParams['axes3d.automargin'] = False # default in 3.8.0
15+
axs[0].set(xlim=(0, 1), ylim=(0, 1), zlim=(0, 1), title='New Behavior')
16+
plt.rcParams['axes3d.automargin'] = True
17+
axs[1].set(xlim=(0, 1), ylim=(0, 1), zlim=(0, 1), title='Old Behavior')

lib/matplotlib/axis.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,14 +829,13 @@ def _get_autoscale_on(self):
829829
def _set_autoscale_on(self, b):
830830
"""
831831
Set whether this Axis is autoscaled when drawing or by
832-
`.Axes.autoscale_view`. If b is None, then the value is not changed.
832+
`.Axes.autoscale_view`.
833833
834834
Parameters
835835
----------
836836
b : bool
837837
"""
838-
if b is not None:
839-
self._autoscale_on = b
838+
self._autoscale_on = b
840839

841840
def get_children(self):
842841
return [self.label, self.offsetText,
@@ -1220,7 +1219,8 @@ def _set_lim(self, v0, v1, *, emit=True, auto):
12201219
# Mark viewlims as no longer stale without triggering an autoscale.
12211220
for ax in self._get_shared_axes():
12221221
ax._stale_viewlims[name] = False
1223-
self._set_autoscale_on(auto)
1222+
if auto is not None:
1223+
self._set_autoscale_on(bool(auto))
12241224

12251225
if emit:
12261226
self.axes.callbacks.process(f"{name}lim_changed", self.axes)

lib/matplotlib/ticker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2077,7 +2077,8 @@ def _raw_ticks(self, vmin, vmax):
20772077

20782078
raw_step = ((_vmax - _vmin) / nbins)
20792079
if hasattr(self.axis, "axes") and self.axis.axes.name == '3d':
2080-
raw_step = raw_step * 23/24 # needed to match mpl3.7 appearance
2080+
# scale factor of 0.9583333333333333 = 23/24 to match mpl3.7
2081+
raw_step = raw_step * 0.9583333333333333
20812082
large_steps = steps >= raw_step
20822083
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
20832084
# Classic round_numbers mode may require a larger step.

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ def __init__(
129129

130130
self.xy_viewLim = Bbox.unit()
131131
self.zz_viewLim = Bbox.unit()
132-
xymargin = 0.05 * 10/11 # match mpl3.7 appearance
132+
133+
# default xymargin of 0.05 * 10/11 is to match mpl3.7 appearance
134+
xymargin = 0.045454545454545454
133135
self.xy_dataLim = Bbox([[xymargin, xymargin],
134136
[1 - xymargin, 1 - xymargin]])
135137
# z-limits are encoded in the x-component of the Bbox, y is un-used
@@ -159,7 +161,8 @@ def __init__(
159161
self.set_axis_on()
160162
self.M = None
161163

162-
self._view_margin = 1/48 # default value to match mpl3.7
164+
# default _view_margin is 1/48 to match mpl37
165+
self._view_margin = 0.020833333333333332
163166
self.autoscale_view()
164167

165168
# func used to format z -- fall back on major formatters
@@ -410,8 +413,9 @@ def set_box_aspect(self, aspect, *, zoom=1):
410413
else:
411414
aspect = np.asarray(aspect, dtype=float)
412415
_api.check_shape((3,), aspect=aspect)
413-
# default scale tuned to match the mpl3.2 appearance.
414-
aspect *= 1.8294640721620434 * 25/24 * zoom / np.linalg.norm(aspect)
416+
# default scale tuned to match the mpl3.2 appearance
417+
# 1.9056917418354617 = 1.8294640721620434 * 25/24
418+
aspect *= 1.9056917418354617 * zoom / np.linalg.norm(aspect)
415419

416420
self._box_aspect = aspect
417421
self.stale = True
@@ -581,17 +585,17 @@ def autoscale(self, enable=True, axis='both', tight=None):
581585
scalez = True
582586
else:
583587
if axis in ['x', 'both']:
584-
self.set_autoscalex_on(enable)
588+
self.set_autoscalex_on(bool(enable))
585589
scalex = self.get_autoscalex_on()
586590
else:
587591
scalex = False
588592
if axis in ['y', 'both']:
589-
self.set_autoscaley_on(enable)
593+
self.set_autoscaley_on(bool(enable))
590594
scaley = self.get_autoscaley_on()
591595
else:
592596
scaley = False
593597
if axis in ['z', 'both']:
594-
self.set_autoscalez_on(enable)
598+
self.set_autoscalez_on(bool(enable))
595599
scalez = self.get_autoscalez_on()
596600
else:
597601
scalez = False
@@ -700,20 +704,61 @@ def _set_bound3d(self, get_bound, set_lim, axis_inverted,
700704
auto=None, view_margin=view_margin)
701705

702706
def set_xbound(self, lower=None, upper=None, view_margin=None):
703-
# docstring inherited
707+
"""
708+
Set the lower and upper numerical bounds of the x-axis.
709+
710+
This method will honor axis inversion regardless of parameter order.
711+
It will not change the autoscaling setting (`.get_autoscalex_on()`).
712+
713+
Parameters
714+
----------
715+
lower, upper : float or None
716+
The lower and upper bounds. If *None*, the respective axis bound
717+
is not modified.
718+
view_margin : float or None
719+
The margin to apply to the bounds. If *None*, the margin is handled
720+
by `set_xlim`.
721+
722+
See Also
723+
--------
724+
get_xbound
725+
get_xlim, set_xlim
726+
invert_xaxis, xaxis_inverted
727+
"""
704728
self._set_bound3d(self.get_xbound, self.set_xlim, self.xaxis_inverted,
705729
lower, upper, view_margin)
706730

707731
def set_ybound(self, lower=None, upper=None, view_margin=None):
708-
# docstring inherited
732+
"""
733+
Set the lower and upper numerical bounds of the y-axis.
734+
735+
This method will honor axis inversion regardless of parameter order.
736+
It will not change the autoscaling setting (`.get_autoscaley_on()`).
737+
738+
Parameters
739+
----------
740+
lower, upper : float or None
741+
The lower and upper bounds. If *None*, the respective axis bound
742+
is not modified.
743+
view_margin : float or None
744+
The margin to apply to the bounds. If *None*, the margin is handled
745+
by `set_ylim`.
746+
747+
See Also
748+
--------
749+
get_ybound
750+
get_ylim, set_ylim
751+
invert_yaxis, yaxis_inverted
752+
"""
709753
self._set_bound3d(self.get_ybound, self.set_ylim, self.yaxis_inverted,
710754
lower, upper, view_margin)
711755

712756
def set_zbound(self, lower=None, upper=None, view_margin=None):
713757
"""
714758
Set the lower and upper numerical bounds of the z-axis.
759+
715760
This method will honor axis inversion regardless of parameter order.
716-
It will not change the autoscaling setting (`.get_autoscaley_on()`).
761+
It will not change the autoscaling setting (`.get_autoscalez_on()`).
717762
718763
Parameters
719764
----------
@@ -722,7 +767,7 @@ def set_zbound(self, lower=None, upper=None, view_margin=None):
722767
is not modified.
723768
view_margin : float or None
724769
The margin to apply to the bounds. If *None*, the margin is handled
725-
by set_zlim.
770+
by `set_zlim`.
726771
727772
See Also
728773
--------
@@ -737,6 +782,7 @@ def _set_lim3d(self, axis, lower=None, upper=None, *, emit=True,
737782
auto=False, view_margin=None, axmin=None, axmax=None):
738783
"""
739784
Set 3D axis limits.
785+
740786
See `.Axes.set_ylim` for full documentation
741787
"""
742788
if upper is None:
@@ -770,6 +816,7 @@ def set_xlim(self, left=None, right=None, *, emit=True, auto=False,
770816
view_margin=None, xmin=None, xmax=None):
771817
"""
772818
Set 3D x limits.
819+
773820
See `~.Axes.set_xlim` for full documentation
774821
"""
775822
return self._set_lim3d(self.xaxis, left, right, emit=emit, auto=auto,
@@ -779,6 +826,7 @@ def set_ylim(self, bottom=None, top=None, *, emit=True, auto=False,
779826
view_margin=None, ymin=None, ymax=None):
780827
"""
781828
Set 3D y limits.
829+
782830
See `~.Axes.set_ylim` for full documentation
783831
"""
784832
return self._set_lim3d(self.yaxis, bottom, top, emit=emit, auto=auto,
@@ -788,6 +836,7 @@ def set_zlim(self, bottom=None, top=None, *, emit=True, auto=False,
788836
view_margin=None, zmin=None, zmax=None):
789837
"""
790838
Set 3D z limits.
839+
791840
See `~.Axes.set_ylim` for full documentation
792841
"""
793842
return self._set_lim3d(self.zaxis, bottom, top, emit=emit, auto=auto,
@@ -1080,12 +1129,15 @@ def clear(self):
10801129
else:
10811130
self._zmargin = 0.
10821131

1083-
xymargin = 0.05 * 10/11 # match mpl3.7 appearance
1132+
# default xymargin of 0.05 * 10/11 is to match mpl3.7 appearance
1133+
xymargin = 0.045454545454545454
10841134
self.xy_dataLim = Bbox([[xymargin, xymargin],
10851135
[1 - xymargin, 1 - xymargin]])
10861136
# z-limits are encoded in the x-component of the Bbox, y is un-used
10871137
self.zz_dataLim = Bbox.unit()
1088-
self._view_margin = 1/48 # default value to match mpl3.7
1138+
1139+
# default _view_margin is 1/48 to match mpl37
1140+
self._view_margin = 0.020833333333333333
10891141
self.autoscale_view()
10901142

10911143
self.grid(mpl.rcParams['axes3d.grid'])
@@ -2988,7 +3040,7 @@ def errorbar(self, x, y, z, zerr=None, yerr=None, xerr=None, fmt='',
29883040
lower limits. In that case a caret symbol is used to indicate
29893041
this. *lims*-arguments may be scalars, or array-likes of the same
29903042
length as the errors. To use limits with inverted axes,
2991-
`~.Axes.set_xlim`, `~.Axes.set_ylim`, or `~.Axes.set_ylim` must be
3043+
`~.Axes.set_xlim`, `~.Axes.set_ylim`, or `~.Axes.set_zlim` must be
29923044
called before `errorbar`. Note the tricky parameter names: setting
29933045
e.g. *ylolims* to True means that the y-value is a *lower* limit of
29943046
the True value, so, only an *upward*-pointing arrow will be drawn!

lib/mpl_toolkits/mplot3d/axis3d.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ def draw(self, renderer):
325325

326326
mins, maxs, tc, highs = self._get_coord_info()
327327
centers = 0.5 * (maxs + mins)
328-
deltas = (maxs - mins) * 1/12 * 23/24 # keep mpl3.7 appearance
328+
# scale factor of 0.0798611111111111 = 1/12 * 23/24
329+
deltas = (maxs - mins) * 0.0798611111111111 # keep mpl3.7 appearance
329330

330331
minmax = np.where(highs, maxs, mins)
331332
maxmin = np.where(~highs, maxs, mins)

0 commit comments

Comments
 (0)