Skip to content

Commit

Permalink
Merge pull request #14652 from anntzer/transform_point
Browse files Browse the repository at this point in the history
Soft-deprecate transform_point.
  • Loading branch information
jklymak committed Jul 5, 2019
2 parents 7976bad + 7b5ca17 commit ace52ff
Show file tree
Hide file tree
Showing 26 changed files with 92 additions and 117 deletions.
4 changes: 2 additions & 2 deletions examples/misc/custom_projection.py
Expand Up @@ -174,8 +174,8 @@ def _set_lim_and_transforms(self):

def _get_affine_transform(self):
transform = self._get_core_transform(1)
xscale, _ = transform.transform_point((np.pi, 0))
_, yscale = transform.transform_point((0, np.pi / 2.0))
xscale, _ = transform.transform((np.pi, 0))
_, yscale = transform.transform((0, np.pi/2))
return Affine2D() \
.scale(0.5 / xscale, 0.5 / yscale) \
.translate(0.5, 0.5)
Expand Down
4 changes: 2 additions & 2 deletions examples/pyplots/annotate_transform.py
Expand Up @@ -19,7 +19,7 @@
ax.set_ylim(-1, 1)

xdata, ydata = 5, 0
xdisplay, ydisplay = ax.transData.transform_point((xdata, ydata))
xdisplay, ydisplay = ax.transData.transform((xdata, ydata))

bbox = dict(boxstyle="round", fc="0.8")
arrowprops = dict(
Expand Down Expand Up @@ -52,6 +52,6 @@
# in this example:

import matplotlib
matplotlib.transforms.Transform.transform_point
matplotlib.transforms.Transform.transform
matplotlib.axes.Axes.annotate
matplotlib.pyplot.annotate
3 changes: 1 addition & 2 deletions lib/matplotlib/axes/_base.py
Expand Up @@ -4046,8 +4046,7 @@ def _set_view_from_bbox(self, bbox, direction='in',

# zoom to rect
inverse = self.transData.inverted()
lastx, lasty = inverse.transform_point((lastx, lasty))
x, y = inverse.transform_point((x, y))
(lastx, lasty), (x, y) = inverse.transform([(lastx, lasty), (x, y)])

if twinx:
x0, x1 = Xmin, Xmax
Expand Down
14 changes: 6 additions & 8 deletions lib/matplotlib/axis.py
Expand Up @@ -737,8 +737,8 @@ class Axis(martist.Artist):
OFFSETTEXTPAD = 3

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

def __init__(self, axes, pickradius=15):
"""
Expand Down Expand Up @@ -1929,11 +1929,10 @@ def contains(self, mouseevent):
x, y = mouseevent.x, mouseevent.y
try:
trans = self.axes.transAxes.inverted()
xaxes, yaxes = trans.transform_point((x, y))
xaxes, yaxes = trans.transform((x, y))
except ValueError:
return False, {}
l, b = self.axes.transAxes.transform_point((0, 0))
r, t = self.axes.transAxes.transform_point((1, 1))
(l, b), (r, t) = self.axes.transAxes.transform([(0, 0), (1, 1)])
inaxis = 0 <= xaxes <= 1 and (
b - self.pickradius < y < b or
t < y < t + self.pickradius)
Expand Down Expand Up @@ -2215,11 +2214,10 @@ def contains(self, mouseevent):
x, y = mouseevent.x, mouseevent.y
try:
trans = self.axes.transAxes.inverted()
xaxes, yaxes = trans.transform_point((x, y))
xaxes, yaxes = trans.transform((x, y))
except ValueError:
return False, {}
l, b = self.axes.transAxes.transform_point((0, 0))
r, t = self.axes.transAxes.transform_point((1, 1))
(l, b), (r, t) = self.axes.transAxes.transform([(0, 0), (1, 1)])
inaxis = 0 <= yaxes <= 1 and (
l - self.pickradius < x < l or
r < x < r + self.pickradius)
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/backend_bases.py
Expand Up @@ -415,8 +415,8 @@ def _iter_collection(self, gc, master_transform, all_transforms,
master_transform)
else:
transform = master_transform
xo, yo = transform.transform_point((xo, yo))
xp, yp = transform.transform_point((0, 0))
(xo, yo), (xp, yp) = transform.transform(
[(xo, yo), (0, 0)])
xo = -(xp - xo)
yo = -(yp - yo)
if not (np.isfinite(xo) and np.isfinite(yo)):
Expand Down Expand Up @@ -1335,7 +1335,7 @@ def __init__(self, name, canvas, x, y, guiEvent=None):
if self.inaxes is not None:
try:
trans = self.inaxes.transData.inverted()
xdata, ydata = trans.transform_point((x, y))
xdata, ydata = trans.transform((x, y))
except ValueError:
pass
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_pdf.py
Expand Up @@ -2059,7 +2059,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
if elt[0] == 'font':
self.file.output(elt[1], elt[2], Op.selectfont)
elif elt[0] == 'text':
curx, cury = mytrans.transform_point((elt[1], elt[2]))
curx, cury = mytrans.transform((elt[1], elt[2]))
self._setup_textpos(curx, cury, angle, oldx, oldy)
oldx, oldy = curx, cury
if len(elt[3]) == 1:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_pgf.py
Expand Up @@ -696,7 +696,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
# if text anchoring can be supported, get the original coordinates
# and add alignment information
pos = mtext.get_unitless_position()
x, y = mtext.get_transform().transform_point(pos)
x, y = mtext.get_transform().transform(pos)
text_args.append("x=%fin" % (x * f))
text_args.append("y=%fin" % (y * f))

Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/backends/backend_svg.py
Expand Up @@ -1037,8 +1037,7 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):

# Get anchor coordinates.
transform = mtext.get_transform()
ax, ay = transform.transform_point(
mtext.get_unitless_position())
ax, ay = transform.transform(mtext.get_unitless_position())
ay = self.height - ay

# Don't do vertical anchor alignment. Most applications do not
Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/contour.py
Expand Up @@ -402,7 +402,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
return rotation, nlc

def _get_label_text(self, x, y, rotation):
dx, dy = self.ax.transData.inverted().transform_point((x, y))
dx, dy = self.ax.transData.inverted().transform((x, y))
t = text.Text(dx, dy, rotation=rotation,
horizontalalignment='center',
verticalalignment='center')
Expand All @@ -414,7 +414,7 @@ def _get_label_clabeltext(self, x, y, rotation):
# class. This way, the rotation of the clabel is along the
# contour line always.
transDataInv = self.ax.transData.inverted()
dx, dy = transDataInv.transform_point((x, y))
dx, dy = transDataInv.transform((x, y))
drotation = transDataInv.transform_angles(np.array([rotation]),
np.array([[x, y]]))
t = ClabelText(dx, dy, rotation=drotation[0],
Expand Down Expand Up @@ -480,7 +480,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
transform = self.ax.transData

if transform:
x, y = transform.transform_point((x, y))
x, y = transform.transform((x, y))

# find the nearest contour _in screen units_
conmin, segmin, imin, xmin, ymin = self.find_nearest_contour(
Expand All @@ -496,7 +496,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
# grab its vertices
lc = active_path.vertices
# sort out where the new vertex should be added data-units
xcmin = self.ax.transData.inverted().transform_point([xmin, ymin])
xcmin = self.ax.transData.inverted().transform([xmin, ymin])
# if there isn't a vertex close enough
if not np.allclose(xcmin, lc[imin]):
# insert new data into the vertex list
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/image.py
Expand Up @@ -906,8 +906,7 @@ def get_cursor_data(self, event):
data_extent = Bbox([[ymin, xmin], [ymax, xmax]])
array_extent = Bbox([[0, 0], arr.shape[:2]])
trans = BboxTransform(boxin=data_extent, boxout=array_extent)
y, x = event.ydata, event.xdata
point = trans.transform_point([y, x])
point = trans.transform([event.ydata, event.xdata])
if any(np.isnan(point)):
return None
i, j = point.astype(int)
Expand Down
9 changes: 2 additions & 7 deletions lib/matplotlib/legend.py
Expand Up @@ -93,15 +93,10 @@ def _update_loc(self, loc_in_canvas):
bbox = self.legend.get_bbox_to_anchor()

_bbox_transform = BboxTransformFrom(bbox)
self.legend._loc = tuple(
_bbox_transform.transform_point(loc_in_canvas)
)
self.legend._loc = tuple(_bbox_transform.transform(loc_in_canvas))

def _update_bbox_to_anchor(self, loc_in_canvas):

tr = self.legend.axes.transAxes
loc_in_bbox = tr.transform_point(loc_in_canvas)

loc_in_bbox = self.legend.axes.transAxes.transform(loc_in_canvas)
self.legend.set_bbox_to_anchor(loc_in_bbox)


Expand Down
15 changes: 7 additions & 8 deletions lib/matplotlib/patches.py
Expand Up @@ -1592,7 +1592,7 @@ def theta_stretch(theta, scale):
theta2 = theta_stretch(self.theta2, width / height)

# Get width and height in pixels
width, height = self.get_transform().transform_point((width, height))
width, height = self.get_transform().transform((width, height))
inv_error = (1.0 / 1.89818e-6) * 0.5
if width < inv_error and height < inv_error:
self._path = Path.arc(theta1, theta2)
Expand Down Expand Up @@ -4168,8 +4168,7 @@ def get_path_in_displaycoord(self):
if self._posA_posB is not None:
posA = self._convert_xy_units(self._posA_posB[0])
posB = self._convert_xy_units(self._posA_posB[1])
posA = self.get_transform().transform_point(posA)
posB = self.get_transform().transform_point(posB)
(posA, posB) = self.get_transform().transform((posA, posB))
_path = self.get_connectionstyle()(posA, posB,
patchA=self.patchA,
patchB=self.patchB,
Expand Down Expand Up @@ -4327,7 +4326,7 @@ def _get_xy(self, x, y, s, axes=None):
trans = axes.transData
x = float(self.convert_xunits(x))
y = float(self.convert_yunits(y))
return trans.transform_point((x, y))
return trans.transform((x, y))
elif s == 'offset points':
# convert the data point
dx, dy = self.xy
Expand All @@ -4353,7 +4352,7 @@ def _get_xy(self, x, y, s, axes=None):
x = r * np.cos(theta)
y = r * np.sin(theta)
trans = axes.transData
return trans.transform_point((x, y))
return trans.transform((x, y))
elif s == 'figure points':
# points from the lower left corner of the figure
dpi = self.figure.dpi
Expand Down Expand Up @@ -4381,7 +4380,7 @@ def _get_xy(self, x, y, s, axes=None):
elif s == 'figure fraction':
# (0,0) is lower left, (1,1) is upper right of figure
trans = self.figure.transFigure
return trans.transform_point((x, y))
return trans.transform((x, y))
elif s == 'axes points':
# points from the lower left corner of the axes
dpi = self.figure.dpi
Expand Down Expand Up @@ -4415,9 +4414,9 @@ def _get_xy(self, x, y, s, axes=None):
elif s == 'axes fraction':
# (0,0) is lower left, (1,1) is upper right of axes
trans = axes.transAxes
return trans.transform_point((x, y))
return trans.transform((x, y))
elif isinstance(s, transforms.Transform):
return s.transform_point((x, y))
return s.transform((x, y))
else:
raise ValueError("{} is not a valid coordinate "
"transformation.".format(s))
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/projections/geo.py
Expand Up @@ -110,8 +110,8 @@ def _set_lim_and_transforms(self):

def _get_affine_transform(self):
transform = self._get_core_transform(1)
xscale, _ = transform.transform_point((np.pi, 0))
_, yscale = transform.transform_point((0, np.pi / 2))
xscale, _ = transform.transform((np.pi, 0))
_, yscale = transform.transform((0, np.pi/2))
return Affine2D() \
.scale(0.5 / xscale, 0.5 / yscale) \
.translate(0.5, 0.5)
Expand Down
14 changes: 7 additions & 7 deletions lib/matplotlib/projections/polar.py
Expand Up @@ -957,12 +957,12 @@ def draw(self, *args, **kwargs):
if isinstance(self.patch, mpatches.Wedge):
# Backwards-compatibility: Any subclassed Axes might override the
# patch to not be the Wedge that PolarAxes uses.
center = self.transWedge.transform_point((0.5, 0.5))
center = self.transWedge.transform((0.5, 0.5))
self.patch.set_center(center)
self.patch.set_theta1(thetamin)
self.patch.set_theta2(thetamax)

edge, _ = self.transWedge.transform_point((1, 0))
edge, _ = self.transWedge.transform((1, 0))
radius = edge - center[0]
width = min(radius * (rmax - rmin) / rmax, radius)
self.patch.set_radius(radius)
Expand Down Expand Up @@ -1419,7 +1419,7 @@ def start_pan(self, x, y, button):
mode = ''
if button == 1:
epsilon = np.pi / 45.0
t, r = self.transData.inverted().transform_point((x, y))
t, r = self.transData.inverted().transform((x, y))
if angle - epsilon <= t <= angle + epsilon:
mode = 'drag_r_labels'
elif button == 3:
Expand All @@ -1441,8 +1441,8 @@ def drag_pan(self, button, key, x, y):
p = self._pan_start

if p.mode == 'drag_r_labels':
startt, startr = p.trans_inverse.transform_point((p.x, p.y))
t, r = p.trans_inverse.transform_point((x, y))
(startt, startr), (t, r) = p.trans_inverse.transform(
[(p.x, p.y), (x, y)])

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

elif p.mode == 'zoom':
startt, startr = p.trans_inverse.transform_point((p.x, p.y))
t, r = p.trans_inverse.transform_point((x, y))
(startt, startr), (t, r) = p.trans_inverse.transform(
[(p.x, p.y), (x, y)])

# Deal with r
scale = r / startr
Expand Down
6 changes: 2 additions & 4 deletions lib/matplotlib/quiver.py
Expand Up @@ -346,7 +346,7 @@ def _text_y(self, y):
def draw(self, renderer):
self._init()
self.vector.draw(renderer)
x, y = self.get_transform().transform_point((self.X, self.Y))
x, y = self.get_transform().transform((self.X, self.Y))
self.text.set_x(self._text_x(x))
self.text.set_y(self._text_y(y))
self.text.draw(renderer)
Expand Down Expand Up @@ -552,9 +552,7 @@ def _init(self):
if True: # not self._initialized:
trans = self._set_transform()
ax = self.ax
sx, sy = trans.inverted().transform_point(
(ax.bbox.width, ax.bbox.height))
self.span = sx
self.span = trans.inverted().transform_bbox(ax.bbox).width
if self.width is None:
sn = np.clip(math.sqrt(self.N), 8, 25)
self.width = 0.06 * self.span / sn
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_artist.py
Expand Up @@ -26,7 +26,7 @@ def test_patch_transform_of_none():

# Draw an ellipse over data coord (2,2) by specifying device coords.
xy_data = (2, 2)
xy_pix = ax.transData.transform_point(xy_data)
xy_pix = ax.transData.transform(xy_data)

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

# draw an ellipse over data coord (2,2) by specifying device coords
xy_data = (2, 2)
xy_pix = ax.transData.transform_point(xy_data)
xy_pix = ax.transData.transform(xy_data)

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

0 comments on commit ace52ff

Please sign in to comment.