Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented inverse transform for Mollweide axes #1624

Merged
merged 4 commits into from Jan 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/matplotlib/projections/geo.py
Expand Up @@ -476,8 +476,16 @@ def __init__(self, resolution):
self._resolution = resolution

def transform_non_affine(self, xy):
# MGDTODO: Math is hard ;(
return xy
x = xy[:, 0:1]
y = xy[:, 1:2]

# from Equations (7, 8) of
# http://mathworld.wolfram.com/MollweideProjection.html
theta = np.arcsin(y / np.sqrt(2))
lon = (np.pi / (2 * np.sqrt(2))) * x / np.cos(theta)
lat = np.arcsin((2 * theta + np.sin(2 * theta)) / np.pi)

return np.concatenate((lon, lat), 1)
transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__

def inverted(self):
Expand Down
43 changes: 43 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Expand Up @@ -933,6 +933,49 @@ def test_transparent_markers():
ax = fig.add_subplot(111)
ax.plot(data, 'D', mfc='none', markersize=100)

@cleanup
def test_mollweide_forward_inverse_closure():
# test that the round-trip Mollweide forward->inverse transformation is an
# approximate identity
fig = plt.figure()
ax = fig.add_subplot(111, projection='mollweide')

# set up 1-degree grid in longitude, latitude
lon = np.linspace(-np.pi, np.pi, 360)
lat = np.linspace(-np.pi / 2.0, np.pi / 2.0, 180)
lon, lat = np.meshgrid(lon, lat)
ll = np.vstack((lon.flatten(), lat.flatten())).T

# perform forward transform
xy = ax.transProjection.transform(ll)

# perform inverse transform
ll2 = ax.transProjection.inverted().transform(xy)

# compare
np.testing.assert_array_almost_equal(ll, ll2, 3)

@cleanup
def test_mollweide_inverse_forward_closure():
# test that the round-trip Mollweide inverse->forward transformation is an
# approximate identity
fig = plt.figure()
ax = fig.add_subplot(111, projection='mollweide')

# set up grid in x, y
x = np.linspace(0, 1, 500)
x, y = np.meshgrid(x, x)
xy = np.vstack((x.flatten(), y.flatten())).T

# perform inverse transform
ll = ax.transProjection.inverted().transform(xy)

# perform forward transform
xy2 = ax.transProjection.transform(ll)

# compare
np.testing.assert_array_almost_equal(xy, xy2, 3)

if __name__=='__main__':
import nose
nose.runmodule(argv=['-s','--with-doctest'], exit=False)