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

Skip to content

Commit df9c152

Browse files
committed
Fix axis inversion with loglocator and logitlocator.
1 parent dc1bf99 commit df9c152

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
@@ -47,9 +47,3 @@ properties of the `.Collection` object:
4747
While this seems complicated, the logic is simply to use the information from
4848
the object that are in data space for the limits, but not information that is
4949
in physical units.
50-
51-
LogLocator.nonsingular now maintains the orders of its arguments
52-
````````````````````````````````````````````````````````````````
53-
54-
It no longer reorders them in increasing order. The new behavior is consistent
55-
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
@@ -3205,8 +3205,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
32053205
cbook._warn_external(
32063206
f"Attempting to set identical left == right == {left} results "
32073207
f"in singular transformations; automatically expanding.")
3208+
swapped = left > right
32083209
left, right = self.xaxis.get_major_locator().nonsingular(left, right)
32093210
left, right = self.xaxis.limit_range_for_scale(left, right)
3211+
if swapped:
3212+
left, right = right, left
32103213

32113214
self._viewLim.intervalx = (left, right)
32123215
if auto is not None:
@@ -3585,8 +3588,11 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
35853588
f"Attempting to set identical bottom == top == {bottom} "
35863589
f"results in singular transformations; automatically "
35873590
f"expanding.")
3591+
swapped = bottom > top
35883592
bottom, top = self.yaxis.get_major_locator().nonsingular(bottom, top)
35893593
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
3594+
if swapped:
3595+
bottom, top = top, bottom
35903596

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

lib/matplotlib/dates.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,9 @@ def nonsingular(self, vmin, vmax):
11171117
"""
11181118
Given the proposed upper and lower extent, adjust the range
11191119
if it is too close to being singular (i.e. a range of ~0).
1120-
11211120
"""
1121+
if vmax < vmin:
1122+
vmin, vmax = vmax, vmin
11221123
unit = self._get_unit()
11231124
interval = self._get_interval()
11241125
if abs(vmax - vmin) < 1e-6:
@@ -1336,6 +1337,8 @@ def tick_values(self, vmin, vmax):
13361337
def nonsingular(self, vmin, vmax):
13371338
# whatever is thrown at us, we can scale the unit.
13381339
# But default nonsingular date plots at an ~4 year period.
1340+
if vmax < vmin:
1341+
vmin, vmax = vmax, vmin
13391342
if vmin == vmax:
13401343
vmin = vmin - DAYS_PER_YEAR * 2
13411344
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
@@ -1697,8 +1697,8 @@ def raise_if_exceeds(self, locs):
16971697
return locs
16981698

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

17031703
def view_limits(self, vmin, vmax):
17041704
"""
@@ -2495,9 +2495,7 @@ def view_limits(self, vmin, vmax):
24952495
return vmin, vmax
24962496

24972497
def nonsingular(self, vmin, vmax):
2498-
swap_vlims = False
24992498
if vmin > vmax:
2500-
swap_vlims = True
25012499
vmin, vmax = vmax, vmin
25022500
if not np.isfinite(vmin) or not np.isfinite(vmax):
25032501
vmin, vmax = 1, 10 # Initial range, no data plotted yet.
@@ -2515,8 +2513,6 @@ def nonsingular(self, vmin, vmax):
25152513
if vmin == vmax:
25162514
vmin = _decade_less(vmin, self._base)
25172515
vmax = _decade_greater(vmax, self._base)
2518-
if swap_vlims:
2519-
vmin, vmax = vmax, vmin
25202516
return vmin, vmax
25212517

25222518

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,11 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False,
616616
cbook._warn_external(
617617
f"Attempting to set identical left == right == {left} results "
618618
f"in singular transformations; automatically expanding.")
619+
swapped = left > right
619620
left, right = self.xaxis.get_major_locator().nonsingular(left, right)
620621
left, right = self.xaxis.limit_range_for_scale(left, right)
622+
if swapped:
623+
left, right = right, left
621624
self.xy_viewLim.intervalx = (left, right)
622625

623626
if auto is not None:
@@ -674,8 +677,11 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False,
674677
f"Attempting to set identical bottom == top == {bottom} "
675678
f"results in singular transformations; automatically "
676679
f"expanding.")
680+
swapped = bottom > top
677681
bottom, top = self.yaxis.get_major_locator().nonsingular(bottom, top)
678682
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
683+
if swapped:
684+
bottom, top = top, bottom
679685
self.xy_viewLim.intervaly = (bottom, top)
680686

681687
if auto is not None:
@@ -732,8 +738,11 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False,
732738
f"Attempting to set identical bottom == top == {bottom} "
733739
f"results in singular transformations; automatically "
734740
f"expanding.")
741+
swapped = bottom > top
735742
bottom, top = self.zaxis.get_major_locator().nonsingular(bottom, top)
736743
bottom, top = self.zaxis.limit_range_for_scale(bottom, top)
744+
if swapped:
745+
bottom, top = top, bottom
737746
self.zz_viewLim.intervalx = (bottom, top)
738747

739748
if auto is not None:

0 commit comments

Comments
 (0)