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

Skip to content

Commit af1ab00

Browse files
committed
Fix axis inversion with loglocator and logitlocator.
1 parent a8f4a74 commit af1ab00

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
API changes
2+
```````````
3+
4+
`Locator.nonsingular` (introduced in mpl 3.1) now returns a range ``v0, v1``
5+
with ``v0 <= v1``. This behavior is consistent with the implementation of
6+
``nonsingular`` by the `LogLocator` and `LogitLocator` subclasses.

lib/matplotlib/axes/_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,8 +3262,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
32623262
cbook._warn_external(
32633263
f"Attempting to set identical left == right == {left} results "
32643264
f"in singular transformations; automatically expanding.")
3265+
swapped = left > right
32653266
left, right = self.xaxis.get_major_locator().nonsingular(left, right)
32663267
left, right = self.xaxis.limit_range_for_scale(left, right)
3268+
if swapped:
3269+
left, right = right, left
32673270

32683271
self.viewLim.intervalx = (left, right)
32693272
if auto is not None:
@@ -3642,8 +3645,11 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
36423645
f"Attempting to set identical bottom == top == {bottom} "
36433646
f"results in singular transformations; automatically "
36443647
f"expanding.")
3648+
swapped = bottom > top
36453649
bottom, top = self.yaxis.get_major_locator().nonsingular(bottom, top)
36463650
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
3651+
if swapped:
3652+
bottom, top = top, bottom
36473653

36483654
self.viewLim.intervaly = (bottom, top)
36493655
if auto is not None:

lib/matplotlib/tests/test_axes.py

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

937937
assert ax.get_xlim() == (-5, 4)
938938
assert ax.get_ylim() == (5, -3)
939-
plt.close()
939+
940+
# Test inverting nonlinear axes.
941+
fig, ax = plt.subplots()
942+
ax.set_yscale("log")
943+
ax.set_ylim(10, 1)
944+
assert ax.get_ylim() == (10, 1)
940945

941946

942947
@image_comparison(baseline_images=['nonfinite_limits'])

lib/matplotlib/ticker.py

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

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

15271527
def view_limits(self, vmin, vmax):
15281528
"""

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,11 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False,
623623
cbook._warn_external(
624624
f"Attempting to set identical left == right == {left} results "
625625
f"in singular transformations; automatically expanding.")
626+
swapped = left > right
626627
left, right = self.xaxis.get_major_locator().nonsingular(left, right)
627628
left, right = self.xaxis.limit_range_for_scale(left, right)
629+
if swapped:
630+
left, right = right, left
628631
self.xy_viewLim.intervalx = (left, right)
629632

630633
if auto is not None:
@@ -681,8 +684,11 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False,
681684
f"Attempting to set identical bottom == top == {bottom} "
682685
f"results in singular transformations; automatically "
683686
f"expanding.")
687+
swapped = bottom > top
684688
bottom, top = self.yaxis.get_major_locator().nonsingular(bottom, top)
685689
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
690+
if swapped:
691+
bottom, top = top, bottom
686692
self.xy_viewLim.intervaly = (bottom, top)
687693

688694
if auto is not None:
@@ -739,8 +745,11 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False,
739745
f"Attempting to set identical bottom == top == {bottom} "
740746
f"results in singular transformations; automatically "
741747
f"expanding.")
748+
swapped = bottom > top
742749
bottom, top = self.zaxis.get_major_locator().nonsingular(bottom, top)
743750
bottom, top = self.zaxis.limit_range_for_scale(bottom, top)
751+
if swapped:
752+
bottom, top = top, bottom
744753
self.zz_viewLim.intervalx = (bottom, top)
745754

746755
if auto is not None:

0 commit comments

Comments
 (0)