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

Skip to content

Commit df667ba

Browse files
authored
Merge pull request #19401 from anntzer/clip_line_to_rect
Simplify axisartist line clipping.
2 parents ca8044c + 62ed6f0 commit df667ba

File tree

6 files changed

+81
-15
lines changed

6 files changed

+81
-15
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``mpl_toolkits.axisartist.clip_path`` is deprecated
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... with no replacement.

lib/mpl_toolkits/axisartist/clip_path.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import math
55

66

7+
_api.warn_deprecated("3.5", name=__name__, obj_type="module")
8+
9+
710
def atan2(dy, dx):
811
if dx == 0 and dy == 0:
912
_api.warn_external("dx and dy are 0")

lib/mpl_toolkits/axisartist/grid_finder.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,48 @@
22

33
from matplotlib import ticker as mticker
44
from matplotlib.transforms import Bbox, Transform
5-
from .clip_path import clip_line_to_rect
5+
6+
7+
def _find_line_box_crossings(xys, bbox):
8+
"""
9+
Find the points where a polyline crosses a bbox, and the crossing angles.
10+
11+
Parameters
12+
----------
13+
xys : (N, 2) array
14+
The polyline coordinates.
15+
bbox : `.Bbox`
16+
The bounding box.
17+
18+
Returns
19+
-------
20+
list of ((float, float), float)
21+
Four separate lists of crossings, for the left, right, bottom, and top
22+
sides of the bbox, respectively. For each list, the entries are the
23+
``((x, y), ccw_angle_in_degrees)`` of the crossing, where an angle of 0
24+
means that the polyline is moving to the right at the crossing point.
25+
26+
The entries are computed by linearly interpolating at each crossing
27+
between the nearest points on either side of the bbox edges.
28+
"""
29+
crossings = []
30+
dxys = xys[1:] - xys[:-1]
31+
for sl in [slice(None), slice(None, None, -1)]:
32+
us, vs = xys.T[sl] # "this" coord, "other" coord
33+
dus, dvs = dxys.T[sl]
34+
umin, vmin = bbox.min[sl]
35+
umax, vmax = bbox.max[sl]
36+
for u0, inside in [(umin, us > umin), (umax, us < umax)]:
37+
crossings.append([])
38+
idxs, = (inside[:-1] ^ inside[1:]).nonzero()
39+
for idx in idxs:
40+
v = vs[idx] + (u0 - us[idx]) * dvs[idx] / dus[idx]
41+
if not vmin <= v <= vmax:
42+
continue
43+
crossing = (u0, v)[sl]
44+
theta = np.degrees(np.arctan2(*dxys[idx][::-1]))
45+
crossings[-1].append((crossing, theta))
46+
return crossings
647

748

849
class ExtremeFinderSimple:
@@ -161,14 +202,12 @@ def _clip_grid_lines_and_find_ticks(self, lines, values, levs, bb):
161202
tck_levels = gi["tick_levels"]
162203
tck_locs = gi["tick_locs"]
163204
for (lx, ly), v, lev in zip(lines, values, levs):
164-
xy, tcks = clip_line_to_rect(lx, ly, bb)
165-
if not xy:
166-
continue
205+
tcks = _find_line_box_crossings(np.column_stack([lx, ly]), bb)
167206
gi["levels"].append(v)
168-
gi["lines"].append(xy)
207+
gi["lines"].append([(lx, ly)])
169208

170209
for tck, direction in zip(tcks,
171-
["left", "bottom", "right", "top"]):
210+
["left", "right", "bottom", "top"]):
172211
for t in tck:
173212
tck_levels[direction].append(lev)
174213
tck_locs[direction].append(t)

lib/mpl_toolkits/tests/test_axisartist_clip_path.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import numpy as np
2+
3+
from matplotlib import _api
24
import matplotlib.pyplot as plt
35
from matplotlib.testing.decorators import image_comparison
46
from matplotlib.transforms import Bbox
57

6-
from mpl_toolkits.axisartist.clip_path import clip_line_to_rect
8+
with _api.suppress_matplotlib_deprecation_warning():
9+
from mpl_toolkits.axisartist.clip_path import clip_line_to_rect
710

811

912
@image_comparison(['clip_path.png'], style='default')

lib/mpl_toolkits/tests/test_axisartist_grid_finder.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1+
import numpy as np
2+
import pytest
3+
4+
from matplotlib.transforms import Bbox
15
from mpl_toolkits.axisartist.grid_finder import (
2-
FormatterPrettyPrint,
3-
MaxNLocator)
6+
_find_line_box_crossings, FormatterPrettyPrint, MaxNLocator)
7+
8+
9+
def test_find_line_box_crossings():
10+
x = np.array([-3, -2, -1, 0., 1, 2, 3, 2, 1, 0, -1, -2, -3, 5])
11+
y = np.arange(len(x))
12+
bbox = Bbox.from_extents(-2, 3, 2, 12.5)
13+
left, right, bottom, top = _find_line_box_crossings(
14+
np.column_stack([x, y]), bbox)
15+
((lx0, ly0), la0), ((lx1, ly1), la1), = left
16+
((rx0, ry0), ra0), ((rx1, ry1), ra1), = right
17+
((bx0, by0), ba0), = bottom
18+
((tx0, ty0), ta0), = top
19+
assert (lx0, ly0, la0) == (-2, 11, 135)
20+
assert (lx1, ly1, la1) == pytest.approx((-2., 12.125, 7.125016))
21+
assert (rx0, ry0, ra0) == (2, 5, 45)
22+
assert (rx1, ry1, ra1) == (2, 7, 135)
23+
assert (bx0, by0, ba0) == (0, 3, 45)
24+
assert (tx0, ty0, ta0) == pytest.approx((1., 12.5, 7.125016))
425

526

627
def test_pretty_print_format():

lib/mpl_toolkits/tests/test_axisartist_grid_helper_curvelinear.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
import platform
32

43
import matplotlib.pyplot as plt
54
from matplotlib.path import Path
@@ -16,8 +15,7 @@
1615
GridHelperCurveLinear
1716

1817

19-
@image_comparison(['custom_transform.png'], style='default',
20-
tol=0.03 if platform.machine() == 'x86_64' else 0.04)
18+
@image_comparison(['custom_transform.png'], style='default', tol=0.2)
2119
def test_custom_transform():
2220
class MyTransform(Transform):
2321
input_dims = output_dims = 2
@@ -79,8 +77,7 @@ def inverted(self):
7977
ax1.grid(True)
8078

8179

82-
@image_comparison(['polar_box.png'], style='default',
83-
tol={'aarch64': 0.04}.get(platform.machine(), 0.03))
80+
@image_comparison(['polar_box.png'], style='default', tol=0.04)
8481
def test_polar_box():
8582
# Remove this line when this test image is regenerated.
8683
plt.rcParams['text.kerning_factor'] = 6
@@ -143,7 +140,7 @@ def test_polar_box():
143140
ax1.grid(True)
144141

145142

146-
@image_comparison(['axis_direction.png'], style='default', tol=0.03)
143+
@image_comparison(['axis_direction.png'], style='default', tol=0.07)
147144
def test_axis_direction():
148145
# Remove this line when this test image is regenerated.
149146
plt.rcParams['text.kerning_factor'] = 6

0 commit comments

Comments
 (0)