Skip to content

Commit 7b5ca17

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 26290b0 commit 7b5ca17

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, _ = transform.transform((np.pi, 0))
178+
_, yscale = transform.transform((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
@@ -4058,8 +4058,7 @@ def _set_view_from_bbox(self, bbox, direction='in',
40584058

40594059
# zoom to rect
40604060
inverse = self.transData.inverted()
4061-
lastx, lasty = inverse.transform_point((lastx, lasty))
4062-
x, y = inverse.transform_point((x, y))
4061+
(lastx, lasty), (x, y) = inverse.transform([(lastx, lasty), (x, y)])
40634062

40644063
if twinx:
40654064
x0, x1 = Xmin, Xmax

lib/matplotlib/axis.py

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

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

743743
def __init__(self, axes, pickradius=15):
744744
"""
@@ -1929,11 +1929,10 @@ def contains(self, mouseevent):
19291929
x, y = mouseevent.x, mouseevent.y
19301930
try:
19311931
trans = self.axes.transAxes.inverted()
1932-
xaxes, yaxes = trans.transform_point((x, y))
1932+
xaxes, yaxes = trans.transform((x, y))
19331933
except ValueError:
19341934
return False, {}
1935-
l, b = self.axes.transAxes.transform_point((0, 0))
1936-
r, t = self.axes.transAxes.transform_point((1, 1))
1935+
(l, b), (r, t) = self.axes.transAxes.transform([(0, 0), (1, 1)])
19371936
inaxis = 0 <= xaxes <= 1 and (
19381937
b - self.pickradius < y < b or
19391938
t < y < t + self.pickradius)
@@ -2218,11 +2217,10 @@ def contains(self, mouseevent):
22182217
x, y = mouseevent.x, mouseevent.y
22192218
try:
22202219
trans = self.axes.transAxes.inverted()
2221-
xaxes, yaxes = trans.transform_point((x, y))
2220+
xaxes, yaxes = trans.transform((x, y))
22222221
except ValueError:
22232222
return False, {}
2224-
l, b = self.axes.transAxes.transform_point((0, 0))
2225-
r, t = self.axes.transAxes.transform_point((1, 1))
2223+
(l, b), (r, t) = self.axes.transAxes.transform([(0, 0), (1, 1)])
22262224
inaxis = 0 <= yaxes <= 1 and (
22272225
l - self.pickradius < x < l or
22282226
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)):
@@ -1335,7 +1335,7 @@ def __init__(self, name, canvas, x, y, guiEvent=None):
13351335
if self.inaxes is not None:
13361336
try:
13371337
trans = self.inaxes.transData.inverted()
1338-
xdata, ydata = trans.transform_point((x, y))
1338+
xdata, ydata = trans.transform((x, y))
13391339
except ValueError:
13401340
pass
13411341
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)

0 commit comments

Comments
 (0)