8000 Merge pull request #1624 from lpsinger/mollweide_inverse_transform · matplotlib/matplotlib@3f76b21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f76b21

Browse files
committed
Merge pull request #1624 from lpsinger/mollweide_inverse_transform
implemented inverse transform for Mollweide axes
2 parents 3b649cb + 6903325 commit 3f76b21

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

lib/matplotlib/projections/geo.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,16 @@ def __init__(self, resolution):
476476
self._resolution = resolution
477477

478478
def transform_non_affine(self, xy):
479-
# MGDTODO: Math is hard ;(
480-
return xy
479+
x = xy[:, 0:1]
480+
y = xy[:, 1:2]
481+
482+
# from Equations (7, 8) of
483+
# http://mathworld.wolfram.com/MollweideProjection.html
484+
theta = np.arcsin(y / np.sqrt(2))
485+
lon = (np.pi / (2 * np.sqrt(2))) * x / np.cos(theta)
486+
lat = np.arcsin((2 * theta + np.sin(2 * theta)) / np.pi)
487+
488+
return np.concatenate((lon, lat), 1)
481489
transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__
482490

483491
def inverted(self):

lib/matplotlib/tests/test_axes.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,49 @@ def test_transparent_markers():
947947
ax = fig.add_subplot(111)
948948
ax.plot(data, 'D', mfc='none', markersize=100)
949949

950+
@cleanup
951+
def test_mollweide_forward_inverse_closure():
952+
# test that the round-trip Mollweide forward->inverse transformation is an
953+
# approximate identity
954+
fig = plt.figure()
955+
ax = fig.add_subplot(111, projection='mollweide')
956+
957+
# set up 1-degree grid in longitude, latitude
958+
lon = np.linspace(-np.pi, np.pi, 360)
959+
lat = np.linspace(-np.pi / 2.0, np.pi / 2.0, 180)
960+
lon, lat = np.meshgrid(lon, lat)
961+
ll = np.vstack((lon.flatten(), lat.flatten())).T
962+
963+
# perform forward transform
964+
xy = ax.transProjection.transform(ll)
965+
966+
# perform inverse transform
967+
ll2 = ax.transProjection.inverted().transform(xy)
968+
969+
# compare
970+
np.testing.assert_array_almost_equal(ll, ll2, 3)
971+
972+
@cleanup
973+
def test_mollweide_inverse_forward_closure():
974+
# test that the round-trip Mollweide inverse->forward transformation is an
975+
# approximate identity
976+
fig = plt.figure()
977+
ax = fig.add_subplot(111, projection='mollweide')
978+
979+
# set up grid in x, y
980+
x = np.linspace(0, 1, 500)
981+
x, y = np.meshgrid(x, x)
982+
xy = np.vstack((x.flatten(), y.flatten())).T
983+
984+
# perform inverse transform
985+
ll = ax.transProjection.inverted().transform(xy)
986+
987+
# perform forward transform
988+
xy2 = ax.transProjection.transform(ll)
989+
990+
# compare
991+
np.testing.assert_array_almost_equal(xy, xy2, 3)
992+
950993
if __name__=='__main__':
951994
import nose
952995
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0