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

Skip to content

Commit 177164d

Browse files
committed
Fix axis inversion with loglocator and logitlocator.
1 parent 5c413df commit 177164d

File tree

7 files changed

+34
-14
lines changed

7 files changed

+34
-14
lines changed

doc/api/next_api_changes/2019-03-04-AL.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,3 @@ without an explicit call to `Axes.autoscale_view`.
1212

1313
In some cases, this can result in different limits being reported. If this is
1414
an issue, consider triggering a draw with `fig.canvas.draw`.
15-
16-
LogLocator.nonsingular now maintains the orders of its arguments
17-
````````````````````````````````````````````````````````````````
18-
19-
It no longer reorders them in increasing order. The new behavior is consistent
20-
with MaxNLocator.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
API changes
2+
```````````
3+
4+
`Locator.nonsingular` (introduced in mpl 3.1), `DateLocator.nonsingular`, and
5+
`AutoDateLocator.nonsingular` now returns a range ``v0, v1`` with ``v0 <= v1``.
6+
This behavior is consistent with the implementation of ``nonsingular`` by the
7+
`LogLocator` and `LogitLocator` subclasses.

lib/matplotlib/axes/_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,8 +3266,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
32663266
cbook._warn_external(
32673267
f"Attempting to set identical left == right == {left} results "
32683268
f"in singular transformations; automatically expanding.")
3269+
swapped = left > right
32693270
left, right = self.xaxis.get_major_locator().nonsingular(left, right)
32703271
left, right = self.xaxis.limit_range_for_scale(left, right)
3272+
if swapped:
3273+
left, right = right, left
32713274

32723275
self._viewLim.intervalx = (left, right)
32733276
if auto is not None:
@@ -3647,8 +3650,11 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
36473650
f"Attempting to set identical bottom == top == {bottom} "
36483651
f"results in singular transformations; automatically "
36493652
f"expanding.")
3653+
swapped = bottom > top
36503654
bottom, top = self.yaxis.get_major_locator().nonsingular(bottom, top)
36513655
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
3656+
if swapped:
3657+
bottom, top = top, bottom
36523658

36533659
self._viewLim.intervaly = (bottom, top)
36543660
if auto is not None:

lib/matplotlib/dates.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,9 @@ def nonsingular(self, vmin, vmax):
11241124
"""
11251125
Given the proposed upper and lower extent, adjust the range
11261126
if it is too close to being singular (i.e. a range of ~0).
1127-
11281127
"""
1128+
if vmax < vmin:
1129+
vmin, vmax = vmax, vmin
11291130
unit = self._get_unit()
11301131
interval = self._get_interval()
11311132
if abs(vmax - vmin) < 1e-6:
@@ -1342,6 +1343,8 @@ def tick_values(self, vmin, vmax):
13421343
def nonsingular(self, vmin, vmax):
13431344
# whatever is thrown at us, we can scale the unit.
13441345
# But default nonsingular date plots at an ~4 year period.
1346+
if vmax < vmin:
1347+
vmin, vmax = vmax, vmin
13451348
if vmin == vmax:
13461349
vmin = vmin - DAYS_PER_YEAR * 2
13471350
vmax = vmax + DAYS_PER_YEAR * 2

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,12 @@ def test_inverted_limits():
899899

900900
assert ax.get_xlim() == (-5, 4)
901901
assert ax.get_ylim() == (5, -3)
902-
plt.close()
902+
903+
# Test inverting nonlinear axes.
904+
fig, ax = plt.subplots()
905+
ax.set_yscale("log")
906+
ax.set_ylim(10, 1)
907+
assert ax.get_ylim() == (10, 1)
903908

904909

905910
@image_comparison(['nonfinite_limits'])

lib/matplotlib/ticker.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,8 +1520,8 @@ def raise_if_exceeds(self, locs):
15201520
return locs
15211521

15221522
def nonsingular(self, v0, v1):
1523-
"""Modify the endpoints of a range as needed to avoid singularities."""
1524-
return mtransforms.nonsingular(v0, v1, increasing=False, expander=.05)
1523+
"""Expand a range as needed to avoid singularities."""
1524+
return mtransforms.nonsingular(v0, v1, expander=.05)
15251525

15261526
def view_limits(self, vmin, vmax):
15271527
"""
@@ -2317,9 +2317,7 @@ def view_limits(self, vmin, vmax):
23172317
return vmin, vmax
23182318

23192319
def nonsingular(self, vmin, vmax):
2320-
swap_vlims = False
23212320
if vmin > vmax:
2322-
swap_vlims = True
23232321
vmin, vmax = vmax, vmin
23242322
if not np.isfinite(vmin) or not np.isfinite(vmax):
23252323
vmin, vmax = 1, 10 # Initial range, no data plotted yet.
@@ -2337,8 +2335,6 @@ def nonsingular(self, vmin, vmax):
23372335
if vmin == vmax:
23382336
vmin = _decade_less(vmin, self._base)
23392337
vmax = _decade_greater(vmax, self._base)
2340-
if swap_vlims:
2341-
vmin, vmax = vmax, vmin
23422338
return vmin, vmax
23432339

23442340

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,11 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False,
632632
cbook._warn_external(
633633
f"Attempting to set identical left == right == {left} results "
634634
f"in singular transformations; automatically expanding.")
635+
swapped = left > right
635636
left, right = self.xaxis.get_major_locator().nonsingular(left, right)
636637
left, right = self.xaxis.limit_range_for_scale(left, right)
638+
if swapped:
639+
left, right = right, left
637640
self.xy_viewLim.intervalx = (left, right)
638641

639642
if auto is not None:
@@ -690,8 +693,11 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False,
690693
f"Attempting to set identical bottom == top == {bottom} "
691694
f"results in singular transformations; automatically "
692695
f"expanding.")
696+
swapped = bottom > top
693697
bottom, top = self.yaxis.get_major_locator().nonsingular(bottom, top)
694698
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
699+
if swapped:
700+
bottom, top = top, bottom
695701
self.xy_viewLim.intervaly = (bottom, top)
696702

697703
if auto is not None:
@@ -748,8 +754,11 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False,
748754
f"Attempting to set identical bottom == top == {bottom} "
749755
f"results in singular transformations; automatically "
750756
f"expanding.")
757+
swapped = bottom > top
751758
bottom, top = self.zaxis.get_major_locator().nonsingular(bottom, top)
752759
bottom, top = self.zaxis.limit_range_for_scale(bottom, top)
760+
if swapped:
761+
bottom, top = top, bottom
753762
self.zz_viewLim.intervalx = (bottom, top)
754763

755764
if auto is not None:

0 commit comments

Comments
 (0)