Skip to content

Commit 9d97a21

Browse files
pelsontacaswell
authored andcommitted
Merge pull request #3863 from maxalbert/fix_log_transforms
Fix log transforms (fixes #3809). Conflicts: lib/matplotlib/tests/test_transforms.py cherry-pick diff picked up too many tests in test_transforms.py
1 parent 408fed6 commit 9d97a21

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/matplotlib/tests/test_transforms.py

+9
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,15 @@ def assert_bbox_eq(bbox1, bbox2):
475475
assert_bbox_eq(inter(r1, r5), bbox_from_ext(1, 1, 1, 1))
476476

477477

478+
@cleanup
479+
def test_log_transform():
480+
# Tests that the last line runs without exception (previously the
481+
# transform would fail if one of the axes was logarithmic).
482+
fig, ax = plt.subplots()
483+
ax.set_yscale('log')
484+
ax.transData.transform((1,1))
485+
486+
478487
if __name__=='__main__':
479488
import nose
480489
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

lib/matplotlib/transforms.py

+34-3
Original file line numberDiff line numberDiff line change
@@ -1277,8 +1277,33 @@ def transform(self, values):
12771277
12781278
Accepts a numpy array of shape (N x :attr:`input_dims`) and
12791279
returns a numpy array of shape (N x :attr:`output_dims`).
1280-
"""
1281-
return self.transform_affine(self.transform_non_affine(values))
1280+
1281+
Alternatively, accepts a numpy array of length :attr:`input_dims`
1282+
and returns a numpy array of length :attr:`output_dims`.
1283+
"""
1284+
# Ensure that values is a 2d array (but remember whether
1285+
# we started with a 1d or 2d array).
1286+
values = np.asanyarray(values)
1287+
ndim = values.ndim
1288+
values = values.reshape((-1, self.input_dims))
1289+
1290+
# Transform the values
1291+
res = self.transform_affine(self.transform_non_affine(values))
1292+
1293+
# Convert the result back to the shape of the input values.
1294+
if ndim == 0:
1295+
assert not np.ma.is_masked(res) # just to be on the safe side
1296+
return res[0, 0]
1297+
if ndim == 1:
1298+
return res.reshape(-1)
1299+
elif ndim == 2:
1300+
return res
1301+
else:
1302+
raise ValueError(
1303+
"Input values must have shape (N x {dims}) "
1304+
"or ({dims}).".format(dims=self.input_dims))
1305+
1306+
return res
12821307

12831308
def transform_affine(self, values):
12841309
"""
@@ -1294,6 +1319,9 @@ def transform_affine(self, values):
12941319
12951320
Accepts a numpy array of shape (N x :attr:`input_dims`) and
12961321
returns a numpy array of shape (N x :attr:`output_dims`).
1322+
1323+
Alternatively, accepts a numpy array of length :attr:`input_dims`
1324+
and returns a numpy array of length :attr:`output_dims`.
12971325
"""
12981326
return self.get_affine().transform(values)
12991327

@@ -1310,6 +1338,9 @@ def transform_non_affine(self, values):
13101338
13111339
Accepts a numpy array of shape (N x :attr:`input_dims`) and
13121340
returns a numpy array of shape (N x :attr:`output_dims`).
1341+
1342+
Alternatively, accepts a numpy array of length :attr:`input_dims`
1343+
and returns a numpy array of length :attr:`output_dims`.
13131344
"""
13141345
return values
13151346

@@ -1935,7 +1966,7 @@ def get_matrix(self):
19351966
get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__
19361967

19371968
def transform(self, points):
1938-
return points
1969+
return np.asanyarray(points)
19391970
transform.__doc__ = Affine2DBase.transform.__doc__
19401971

19411972
transform_affine = transform

0 commit comments

Comments
 (0)