8000 Soft-deprecate transform_point. · matplotlib/matplotlib@29eaa38 · GitHub
[go: up one dir, main page]

Skip to content

Commit 29eaa38

Browse files
committed
Soft-deprecate transform_point.
The transform() method has explicit code to handle 1d input, so transform_point() is redundant with transform() (... and is anyways implemented in terms of transform(), so not faster either)... and longer to type. Replace it throughout the codebase. Also compress consecutive calls to transform_point() with a single call to transform(), which may be a bit faster (one matrix multiplication instead of two); also a single replacement to use transform_bbox as appropriate. Only "deprecate" transform_point in the docs because I don't really want to discuss here whether this would be too disruptive...
1 parent db06a34 commit 29eaa38

26 files changed

+92
-117
lines changed

examples/misc/custom_projection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ def _set_lim_and_transforms(self):
174174

175175
def _get_affine_transform(self):
176176
transform = self._get_core_transform(1)
177-
xscale, _ = transform.transform_point((np.pi, 0))
178-
_, yscale = transform.transform_point((0, np.pi / 2.0))
177+
(xscale, _), (_, yscale) = transform.transform(
178+
[(np.pi, 0), (0, np.pi/2)])
179179
return Affine2D() \
180180
.scale(0.5 / xscale, 0.5 / yscale) \
181181
.translate(0.5, 0.5)

examples/pyplots/annotate_transform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
ax.set_ylim(-1, 1)
2020

2121
xdata, ydata = 5, 0
22-
xdisplay, ydisplay = ax.transData.transform_point((xdata, ydata))
22+
xdisplay, ydisplay = ax.transData.transform((xdata, ydata))
2323

2424
bbox = dict(boxstyle="round", fc="0.8")
2525
arrowprops = dict(
@@ -52,6 +52,6 @@
5252
# in this example:
5353

5454
import matplotlib
55-
matplotlib.transforms.Transform.transform_point
55+
matplotlib.transforms.Transform.transform
5656
matplotlib.axes.Axes.annotate
5757
matplotlib.pyplot.annotate

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,8 +4052,7 @@ def _set_view_from_bbox(self, bbox, direction='in',
40524052

40534053
# zoom to rect
40544054
inverse = self.transData.inverted()
4055-
lastx, lasty = inverse.transform_point((lastx, lasty))
4056-
x, y = inverse.transform_point((x, y))
4055+
(lastx, lasty), (x, y) = inverse.transform([(lastx, lasty), (x, y)])
40574056

40584057
if twinx:
40594058
x0, x1 = Xmin, Xmax

lib/matplotlib/axis.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,8 @@ class Axis(martist.Artist):
738738
OFFSETTEXTPAD = 3
739739

740740
def __str__(self):
741-
return self.__class__.__name__ \
742-
+ "(%f,%f)" % tuple(self.axes.transAxes.transform_point((0, 0)))
741+
return "{} ({},{})".format(
742+
type(self).__name__, *self.axes.transAxes.transform((0, 0)))
743743

744744
def __init__(self, axes, pickradius=15):
745745
"""
@@ -1930,11 +1930,10 @@ def contains(self, mouseevent):
19301930
x, y = mouseevent.x, mouseevent.y
19311931
try:
19321932
trans = self.axes.transAxes.inverted()
1933-
xaxes, yaxes = trans.transform_point((x, y))
1933+
xaxes, yaxes = trans.transform((x, y))
19341934
except ValueError:
19351935
return False, {}
1936-
l, b = self.axes.transAxes.transform_point((0, 0))
1937-
r, t = self.axes.transAxes.transform_point((1, 1))
1936+
(l, b), (r, t) = self.axes.transAxes.transform([(0, 0), (1, 1)])
19381937
inaxis = 0 <= xaxes <= 1 and (
19391938
b - self.pickradius < y < b or
19401939
t < y < t + self.pickradius)
@@ -2219,11 +2218,10 @@ def contains(self, mouseevent):
22192218
x, y = mouseevent.x, mouseevent.y
22202219
try:
22212220
trans = self.axes.transAxes.inverted()
2222-
xaxes, yaxes = trans.transform_point((x, y))
2221+
xaxes, yaxes = trans.transform((x, y))
22232222
except ValueError:
22242223
return False, {}
2225-
l, b = self.axes.transAxes.transform_point((0, 0))
2226-
r, t = self.axes.transAxes.transform_point((1, 1))
2224+
(l, b), (r, t) = self.axes.transAxes.transform([(0, 0), (1, 1)])
22272225
inaxis = 0 <= yaxes <= 1 and (
22282226
l - self.pickradius < x < l or
22292227
r < x < r + self.pickradius)

lib/matplotlib/backend_bases.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ def _iter_collection(self, gc, master_transform, all_transforms,
415415
master_transform)
416416
else:
417417
transform = master_transform
418-
xo, yo = transform.transform_point((xo, yo))
419-
xp, yp = transform.transform_point((0, 0))
418+
(xo, yo), (xp, yp) = transform.transform(
419+
[(xo, yo), (0, 0)])
420420
xo = -(xp - xo)
421421
yo = -(yp - yo)
422422
if not (np.isfinite(xo) and np.isfinite(yo)):
@@ -1334,7 +1334,7 @@ def __init__(self, name, canvas, x, y, guiEvent=None):
13341334
if self.inaxes is not None:
13351335
try:
13361336
trans = self.inaxes.transData.inverted()
1337-
xdata, ydata = trans.transform_point((x, y))
1337+
xdata, ydata = trans.transform((x, y))
13381338
except ValueError:
13391339
pass
13401340
else:

lib/matplotlib/backends/backend_pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
20602060
if elt[0] == 'font':
20612061
self.file.output(elt[1], elt[2], Op.selectfont)
20622062
elif elt[0] == 'text':
2063-
curx, cury = mytrans.transform_point((elt[1], elt[2]))
2063+
curx, cury = mytrans.transform((elt[1], elt[2]))
20642064
self._setup_textpos(curx, cury, angle, oldx, oldy)
20652065
oldx, oldy = curx, cury
20662066
if len(elt[3]) == 1:

lib/matplotlib/backends/backend_pgf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
696696
# if text anchoring can be supported, get the original coordinates
697697
# and add alignment information
698698
pos = mtext.get_unitless_position()
699-
x, y = mtext.get_transform().transform_point(pos)
699+
x, y = mtext.get_transform().transform(pos)
700700
text_args.append("x=%fin" % (x * f))
701701
text_args.append("y=%fin" % (y * f))
702702

lib/matplotlib/backends/backend_svg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,8 +1037,7 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
10371037

10381038
# Get anchor coordinates.
10391039
transform = mtext.get_transform()
1040-
ax, ay = transform.transform_point(
1041-
mtext.get_unitless_position())
1040+
ax, ay = transform.transform(mtext.get_unitless_position())
10421041
ay = self.height - ay
10431042

10441043
# Don't do vertical anchor alignment. Most applications do not

lib/matplotlib/contour.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
402402
return rotation, nlc
403403

404404
def _get_label_text(self, x, y, rotation):
405-
dx, dy = self.ax.transData.inverted().transform_point((x, y))
405+
dx, dy = self.ax.transData.inverted().transform((x, y))
406406
t = text.Text(dx, dy, rotation=rotation,
407407
horizontalalignment='center',
408408
verticalalignment='center')
@@ -414,7 +414,7 @@ def _get_label_clabeltext(self, x, y, rotation):
414414
# class. This way, the rotation of the clabel is along the
415415
# contour line always.
416416
transDataInv = self.ax.transData.inverted()
417-
dx, dy = transDataInv.transform_point((x, y))
417+
dx, dy = transDataInv.transform((x, y))
418418
drotation = transDataInv.transform_angles(np.array([rotation]),
419419
np.array([[x, y]]))
420420
t = ClabelText(dx, dy, rotation=drotation[0],
@@ -480,7 +480,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
480480
transform = self.ax.transData
481481

482482
if transform:
483-
x, y = transform.transform_point((x, y))
483+
x, y = transform.transform((x, y))
484484

485485
# find the nearest contour _in screen units_
486486
conmin, segmin, imin, xmin, ymin = self.find_nearest_contour(
@@ -496,7 +496,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
496496
# grab its vertices
497497
lc = active_path.vertices
498498
# sort out where the new vertex should be added data-units
499-
xcmin = self.ax.transData.inverted().transform_point([xmin, ymin])
499+
xcmin = self.ax.transData.inverted().transform([xmin, ymin])
500500
# if there isn't a vertex close enough
501501
if not np.allclose(xcmin, lc[imin]):
502502
# insert new data into the vertex list

lib/matplotlib/image.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,7 @@ def get_cursor_data(self, event):
906906
data_extent = Bbox([[ymin, xmin], [ymax, xmax]])
907907
array_extent = Bbox([[0, 0], arr.shape[:2]])
908908
trans = BboxTransform(boxin=data_extent, boxout=array_extent)
909-
y, x = event.ydata, event.xdata
910-
point = trans.transform_point([y, x])
909+
point = trans.transform([event.ydata, event.xdata])
911910
if any(np.isnan(point)):
912911
return None
913912
i, j = point.astype(int)

lib/matplotlib/legend.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,10 @@ def _update_loc(self, loc_in_canvas):
9393
bbox = self.legend.get_bbox_to_anchor()
9494

9595
_bbox_transform = BboxTransformFrom(bbox)
96-
self.legend._loc = tuple(
97-
_bbox_transform.transform_point(loc_in_canvas)
98-
)
96+
self.legend._loc = tuple(_bbox_transform.transform(loc_in_canvas))
9997

10098
def _update_bbox_to_anchor(self, loc_in_canvas):
101-
102-
tr = self.legend.axes.transAxes
103-
loc_in_bbox = tr.transform_point(loc_in_canvas)
104-
99+
loc_in_bbox = self.legend.axes.transAxes.transform(loc_in_canvas)
105100
self.legend.set_bbox_to_anchor(loc_in_bbox)
106101

107102

lib/matplotlib/patches.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ def theta_stretch(theta, scale):
15921592
theta2 = theta_stretch(self.theta2, width / height)
15931593

15941594
# Get width and height in pixels
1595-
width, height = self.get_transform().transform_point((width, height))
1595+
width, height = self.get_transform().transform((width, height))
15961596
inv_error = (1.0 / 1.89818e-6) * 0.5
15971597
if width < inv_error and height < inv_error:
15981598
self._path = Path.arc(theta1, theta2)
@@ -4168,8 +4168,7 @@ def get_path_in_displaycoord(self):
41684168
if self._posA_posB is not None:
41694169
posA = self._convert_xy_units(self._posA_posB[0])
41704170
posB = self._convert_xy_units(self._posA_posB[1])
4171-
posA = self.get_transform().transform_point(posA)
4172-
posB = self.get_transform().transform_point(posB)
4171+
(posA, posB) = self.get_transform().transform((posA, posB))
41734172
_path = self.get_connectionstyle()(posA, posB,
41744173
patchA=self.patchA,
41754174
patchB=self.patchB,
@@ -4327,7 +4326,7 @@ def _get_xy(self, x, y, s, axes=None):
43274326
trans = axes.transData
43284327
x = float(self.convert_xunits(x))
43294328
y = float(self.convert_yunits(y))
4330-
return trans.transform_point((x, y))
4329+
return trans.transform((x, y))
43314330
elif s == 'offset points':
43324331
# convert the data point
43334332
dx, dy = self.xy
@@ -4353,7 +4352,7 @@ def _get_xy(self, x, y, s, axes=None):
43534352
x = r * np.cos(theta)
43544353
y = r * np.sin(theta)
43554354
trans = axes.transData
4356-
return trans.transform_point((x, y))
4355+
return trans.transform((x, y))
43574356
elif s == 'figure points':
43584357
# points from the lower left corner of the figure
43594358
dpi = self.figure.dpi
@@ -4381,7 +4380,7 @@ def _get_xy(self, x, y, s, axes=None):
43814380
elif s == 'figure fraction':
43824381
# (0,0) is lower left, (1,1) is upper right of figure
43834382
trans = self.figure.transFigure
4384-
return trans.transform_point((x, y))
4383+
return trans.transform((x, y))
43854384
elif s == 'axes points':
43864385
# points from the lower left corner of the axes
43874386
dpi = self.figure.dpi
@@ -4415,9 +4414,9 @@ def _get_xy(self, x, y, s, axes=None):
44154414
elif s == 'axes fraction':
44164415
# (0,0) is lower left, (1,1) is upper right of axes
44174416
trans = axes.transAxes
4418-
return trans.transform_point((x, y))
4417+
return trans.transform((x, y))
44194418
elif isinstance(s, transforms.Transform):
4420-
return s.transform_point((x, y))
4419+
return s.transform((x, y))
44214420
else:
44224421
raise ValueError("{} is not a valid coordinate "
44234422
"transformation.".format(s))

lib/matplotlib/projections/geo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ def _set_lim_and_transforms(self):
111111

112112
def _get_affine_transform(self):
113113
transform = self._get_core_transform(1)
114-
xscale, _ = transform.transform_point((np.pi, 0))
115-
_, yscale = transform.transform_point((0, np.pi / 2))
114+
(xscale, _), (_, yscale) = transform.transform(
115+
[(np.pi, 0), (0, np.pi/2)])
116116
return Affine2D() \
117117
.scale(0.5 / xscale, 0.5 / yscale) \
118118
.translate(0.5, 0.5)

lib/matplotlib/projections/polar.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -957,12 +957,12 @@ def draw(self, *args, **kwargs):
957957
if isinstance(self.patch, mpatches.Wedge):
958958
# Backwards-compatibility: Any subclassed Axes might override the
959959
# patch to not be the Wedge that PolarAxes uses.
960-
center = self.transWedge.transform_point((0.5, 0.5))
960+
center = self.transWedge.transform((0.5, 0.5))
961961
self.patch.set_center(center)
962962
self.patch.set_theta1(thetamin)
963963
self.patch.set_theta2(thetamax)
964964

965-
edge, _ = self.transWedge.transform_point((1, 0))
965+
edge, _ = self.transWedge.transform((1, 0))
966966
radius = edge - center[0]
967967
width = min(radius * (rmax - rmin) / rmax, radius)
968968
self.patch.set_radius(radius)
@@ -1419,7 +1419,7 @@ def start_pan(self, x, y, button):
14191419
mode = ''
14201420
if button == 1:
14211421
epsilon = np.pi / 45.0
1422-
t, r = self.transData.inverted().transform_point((x, y))
1422+
t, r = self.transData.inverted().transform((x, y))
14231423
if angle - epsilon <= t <= angle + epsilon:
14241424
mode = 'drag_r_labels'
14251425
elif button == 3:
@@ -1441,8 +1441,8 @@ def drag_pan(self, button, key, x, y):
14411441
p = self._pan_start
14421442

14431443
if p.mode == 'drag_r_labels':
1444-
startt, startr = p.trans_inverse.transform_point((p.x, p.y))
1445-
t, r = p.trans_inverse.transform_point((x, y))
1444+
(startt, startr), (t, r) = p.trans_inverse.transform(
1445+
[(p.x, p.y), (x, y)])
14461446

14471447
# Deal with theta
14481448
dt0 = t - startt
@@ -1463,8 +1463,8 @@ def drag_pan(self, button, key, x, y):
14631463
t.label2.set_ha(horiz2)
14641464

14651465
elif p.mode == 'zoom':
1466-
startt, startr = p.trans_inverse.transform_point((p.x, p.y))
1467-
t, r = p.trans_inverse.transform_point((x, y))
1466+
(startt, startr), (t, r) = p.trans_inverse.transform(
1467+
[(p.x, p.y), (x, y)])
14681468

14691469
# Deal with r
14701470
scale = r / startr

lib/matplotlib/quiver.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def _text_y(self, y):
346346
def draw(self, renderer):
347347
self._init()
348348
self.vector.draw(renderer)
349-
x, y = self.get_transform().transform_point((self.X, self.Y))
349+
x, y = self.get_transform().transform((self.X, self.Y))
350350
self.text.set_x(self._text_x(x))
351351
self.text.set_y(self._text_y(y))
352352
self.text.draw(renderer)
@@ -552,9 +552,7 @@ def _init(self):
552552
if True: # not self._initialized:
553553
trans = self._set_transform()
554554
ax = self.ax
555-
sx, sy = trans.inverted().transform_point(
556-
(ax.bbox.width, ax.bbox.height))
557-
self.span = sx
555+
self.span = trans.inverted().transform_bbox(ax.bbox).width
558556
if self.width is None:
559557
sn = np.clip(math.sqrt(self.N), 8, 25)
560558
self.width = 0.06 * self.span / sn

lib/matplotlib/tests/test_artist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_patch_transform_of_none():
2626

2727
# Draw an ellipse over data coord (2,2) by specifying device coords.
2828
xy_data = (2, 2)
29-
xy_pix = ax.transData.transform_point(xy_data)
29+
xy_pix = ax.transData.transform(xy_data)
3030

3131
# Not providing a transform of None puts the ellipse in data coordinates .
3232
e = mpatches.Ellipse(xy_data, width=1, height=1, fc='yellow', alpha=0.5)
@@ -68,7 +68,7 @@ def test_collection_transform_of_none():
6868

6969
# draw an ellipse over data coord (2,2) by specifying device coords
7070
xy_data = (2, 2)
71-
xy_pix = ax.transData.transform_point(xy_data)
71+
xy_pix = ax.transData.transform(xy_data)
7272

7373
# not providing a transform of None puts the ellipse in data coordinates
7474
e = mpatches.Ellipse(xy_data, width=1, height=1)

0 commit comments

Comments
 (0)
0