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

Skip to content

Commit 06a13b2

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

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
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: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -919,24 +919,29 @@ def test_hexbin_log():
919919

920920

921921
def test_inverted_limits():
922-
# Test gh:1553
923-
# Calling invert_xaxis prior to plotting should not disable autoscaling
924-
# while still maintaining the inverted direction
922+
# # Test gh:1553
923+
# # Calling invert_xaxis prior to plotting should not disable autoscaling
924+
# # while still maintaining the inverted direction
925+
# fig, ax = plt.subplots()
926+
# ax.invert_xaxis()
927+
# ax.plot([-5, -3, 2, 4], [1, 2, -3, 5])
928+
#
929+
# assert ax.get_xlim() == (4, -5)
930+
# assert ax.get_ylim() == (-3, 5)
931+
# plt.close()
932+
#
933+
# fig, ax = plt.subplots()
934+
# ax.invert_yaxis()
935+
# ax.plot([-5, -3, 2, 4], [1, 2, -3, 5])
936+
#
937+
# assert ax.get_xlim() == (-5, 4)
938+
# assert ax.get_ylim() == (5, -3)
939+
940+
# Test inverting nonlinear axes.
925941
fig, ax = plt.subplots()
926-
ax.invert_xaxis()
927-
ax.plot([-5, -3, 2, 4], [1, 2, -3, 5])
928-
929-
assert ax.get_xlim() == (4, -5)
930-
assert ax.get_ylim() == (-3, 5)
931-
plt.close()
932-
933-
fig, ax = plt.subplots()
934-
ax.invert_yaxis()
935-
ax.plot([-5, -3, 2, 4], [1, 2, -3, 5])
936-
937-
assert ax.get_xlim() == (-5, 4)
938-
assert ax.get_ylim() == (5, -3)
939-
plt.close()
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,11 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False,
739739
f"Attempting to set identical bottom == top == {bottom} "
740740
f"results in singular transformations; automatically "
741741
f"expanding.")
742+
swapped = bottom > top
742743
bottom, top = self.zaxis.get_major_locator().nonsingular(bottom, top)
743744
bottom, top = self.zaxis.limit_range_for_scale(bottom, top)
745+
if swapped:
746+
bottom, top = top, bottom
744747
self.zz_viewLim.intervalx = (bottom, top)
745748

746749
if auto is not None:

0 commit comments

Comments
 (0)