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

Skip to content

Commit 0679e93

Browse files
committed
fix axisartist behavior when axis limit is inverted
1 parent ba4043a commit 0679e93

2 files changed

Lines changed: 69 additions & 7 deletions

File tree

lib/mpl_toolkits/axisartist/axislines.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,39 @@ def get_tightbbox(self, renderer):
767767
return _bbox
768768

769769

770+
def set_xlim(self, left=None, right=None, emit=True, auto=False,
771+
swap_axis=True, **kw):
772+
773+
x1o, x2o = self.get_xlim()
774+
775+
maxes.Axes.set_xlim(self, left, right, emit, auto, **kw)
776+
x1, x2 = self.get_xlim()
777+
778+
if not swap_axis:
779+
return
780+
781+
if (x1o > x2o and x1 < x2) or (x1o < x2o and x1 > x2):
782+
self.axis["right"], self.axis["left"] = self.axis["left"], self.axis["right"]
783+
784+
self.axis["left"].set_axis_direction("left")
785+
self.axis["right"].set_axis_direction("right")
786+
787+
788+
def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
789+
swap_axis=True, **kw):
790+
791+
y1o, y2o = self.get_ylim()
792+
793+
maxes.Axes.set_ylim(self, bottom, top, emit, auto, **kw)
794+
y1, y2 = self.get_ylim()
795+
796+
if y1o > y2o and y1 < y2 or (y1o < y2o and y1 > y2):
797+
self.axis["top"], self.axis["bottom"] = self.axis["bottom"], self.axis["top"]
798+
799+
self.axis["top"].set_axis_direction("top")
800+
self.axis["bottom"].set_axis_direction("bottom")
801+
802+
770803

771804
Subplot = maxes.subplot_class_factory(Axes)
772805

lib/mpl_toolkits/axisartist/clip_path.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@ def atan2(dy, dx):
1212
# FIXME : The current algorithm seems to return incorrect angle when the line
1313
# ends at the boundary.
1414

15-
def clip(xlines, ylines, x0, clip="right"):
15+
def clip(xlines, ylines, x0, clip="right", xdir=True, ydir=True):
1616

1717
clipped_xlines = []
1818
clipped_ylines = []
1919

2020
_pos_angles = []
2121

22+
if xdir:
23+
xsign = 1
24+
else:
25+
xsign = -1
26+
27+
if ydir:
28+
ysign = 1
29+
else:
30+
ysign = -1
31+
32+
2233
for x, y in zip(xlines, ylines):
2334

2435
if clip in ["up", "right"]:
@@ -49,7 +60,7 @@ def clip(xlines, ylines, x0, clip="right"):
4960
dx = x[i+1] - x[i]
5061
dy = y[i+1] - y[i]
5162

52-
a = degrees(atan2(dy, dx))
63+
a = degrees(atan2(ysign*dy, xsign*dx))
5364
_pos_angles.append((x0, y0, a))
5465

5566
elif c == 1:
@@ -63,7 +74,7 @@ def clip(xlines, ylines, x0, clip="right"):
6374
dx = x[i+1] - x[i]
6475
dy = y[i+1] - y[i]
6576

66-
a = degrees(atan2(dy, dx))
77+
a = degrees(atan2(ysign*dy, xsign*dx))
6778
_pos_angles.append((x0, y0, a))
6879

6980
#print x[i], x[i+1]
@@ -82,10 +93,28 @@ def clip_line_to_rect(xline, yline, bbox):
8293

8394
x0, y0, x1, y1 = bbox.extents
8495

85-
lx1, ly1, c_right_ = clip([xline], [yline], x1, clip="right")
86-
lx2, ly2, c_left_ = clip(lx1, ly1, x0, clip="left")
87-
ly3, lx3, c_top_ = clip(ly2, lx2, y1, clip="right")
88-
ly4, lx4, c_bottom_ = clip(ly3, lx3, y0, clip="left")
96+
xdir = x1 > x0
97+
ydir = y1 > y0
98+
99+
if x1 > x0:
100+
lx1, ly1, c_right_ = clip([xline], [yline], x1, clip="right", xdir=xdir, ydir=ydir)
101+
lx2, ly2, c_left_ = clip(lx1, ly1, x0, clip="left", xdir=xdir, ydir=ydir)
102+
else:
103+
lx1, ly1, c_right_ = clip([xline], [yline], x0, clip="right", xdir=xdir, ydir=ydir)
104+
lx2, ly2, c_left_ = clip(lx1, ly1, x1, clip="left", xdir=xdir, ydir=ydir)
105+
106+
if y1 > y0:
107+
ly3, lx3, c_top_ = clip(ly2, lx2, y1, clip="right", xdir=ydir, ydir=xdir)
108+
ly4, lx4, c_bottom_ = clip(ly3, lx3, y0, clip="left", xdir=ydir, ydir=xdir)
109+
else:
110+
ly3, lx3, c_top_ = clip(ly2, lx2, y0, clip="right", xdir=ydir, ydir=xdir)
111+
ly4, lx4, c_bottom_ = clip(ly3, lx3, y1, clip="left", xdir=ydir, ydir=xdir)
112+
113+
114+
# lx1, ly1, c_right_ = clip([xline], [yline], x1, clip="right")
115+
# lx2, ly2, c_left_ = clip(lx1, ly1, x0, clip="left")
116+
# ly3, lx3, c_top_ = clip(ly2, lx2, y1, clip="right")
117+
# ly4, lx4, c_bottom_ = clip(ly3, lx3, y0, clip="left")
89118

90119
#c_left = [((x, y), (a+90)%180-180) for (x, y, a) in c_left_ \
91120
# if bbox.containsy(y)]

0 commit comments

Comments
 (0)