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

Skip to content

Commit 543a222

Browse files
committed
Merge pull request #3863 from maxalbert/fix_log_transforms
Fix log transforms (fixes #3809).
2 parents 59eda22 + 0b43f83 commit 543a222

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

lib/matplotlib/tests/test_transforms.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,16 @@ def test_transform_single_point():
492492
r = t.transform_affine((1, 1))
493493
assert r.shape == (2,)
494494

495+
496+
@cleanup
497+
def test_log_transform():
498+
# Tests that the last line runs without exception (previously the
499+
# transform would fail if one of the axes was logarithmic).
500+
fig, ax = plt.subplots()
501+
ax.set_yscale('log')
502+
ax.transData.transform((1,1))
503+
504+
495505
if __name__=='__main__':
496506
import nose
497507
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

lib/matplotlib/transforms.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,33 @@ def transform(self, values):
12851285
12861286
Accepts a numpy array of shape (N x :attr:`input_dims`) and
12871287
returns a numpy array of shape (N x :attr:`output_dims`).
1288-
"""
1289-
return self.transform_affine(self.transform_non_affine(values))
1288+
1289+
Alternatively, accepts a numpy array of length :attr:`input_dims`
1290+
and returns a numpy array of length :attr:`output_dims`.
1291+
"""
1292+
# Ensure that values is a 2d array (but remember whether
1293+
# we started with a 1d or 2d array).
1294+
values = np.asanyarray(values)
1295+
ndim = values.ndim
1296+
values = values.reshape((-1, self.input_dims))
1297+
1298+
# Transform the values
1299+
res = self.transform_affine(self.transform_non_affine(values))
1300+
1301+
# Convert the result back to the shape of the input values.
1302+
if ndim == 0:
1303+
assert not np.ma.is_masked(res) # just to be on the safe side
1304+
return res[0, 0]
1305+
if ndim == 1:
1306+
return res.reshape(-1)
1307+
elif ndim == 2:
1308+
return res
1309+
else:
1310+
raise ValueError(
1311+
"Input values must have shape (N x {dims}) "
1312+
"or ({dims}).".format(dims=self.input_dims))
1313+
1314+
return res
12901315

12911316
def transform_affine(self, values):
12921317
"""
@@ -1302,6 +1327,9 @@ def transform_affine(self, values):
13021327
13031328
Accepts a numpy array of shape (N x :attr:`input_dims`) and
13041329
returns a numpy array of shape (N x :attr:`output_dims`).
1330+
1331+
Alternatively, accepts a numpy array of length :attr:`input_dims`
1332+
and returns a numpy array of length :attr:`output_dims`.
13051333
"""
13061334
return self.get_affine().transform(values)
13071335

@@ -1318,6 +1346,9 @@ def transform_non_affine(self, values):
13181346
13191347
Accepts a numpy array of shape (N x :attr:`input_dims`) and
13201348
returns a numpy array of shape (N x :attr:`output_dims`).
1349+
1350+
Alternatively, accepts a numpy array of length :attr:`input_dims`
1351+
and returns a numpy array of length :attr:`output_dims`.
13211352
"""
13221353
return values
13231354

@@ -1944,7 +1975,7 @@ def get_matrix(self):
19441975
get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__
19451976

19461977
def transform(self, points):
1947-
return points
1978+
return np.asanyarray(points)
19481979
transform.__doc__ = Affine2DBase.transform.__doc__
19491980

19501981
transform_affine = transform

0 commit comments

Comments
 (0)