Pylab contourf plot using Mollweide projection create artefacts #1910

Closed
ghost opened this Issue Apr 16, 2013 · 9 comments

Comments

Projects
None yet
7 participants
@ghost

ghost commented Apr 16, 2013

matplotlib 1.1.1rc, python 2.7.3, Ubuntu 12.04 LTS

pylab.contourf create artefacts when used in a Mollweide projection.

Here is an example code:

import numpy as np
import pylab as plt

ra = np.linspace(-np.pi, np.pi, 40)
dec= np.linspace(-np.pi/2, np.pi/2, 20)
X,Y = np.meshgrid(ra,dec)
Z = np.sin(X) * np.cos(X) * np.sin(Y) * np.cos(Y)

plt.figure()
ax = plt.subplot(111, projection = 'mollweide')
ax.contourf(X,Y,Z,100)
ax.contour(X,Y,Z,10,colors='k')

plt.show()

Some of the points seem to be wrongly drawn (See Figure below).
mollweide
The same code used without projection outputs a perfectly normal map:
normal

You may have noticed that the contour lines work in both images. The artefacts seem to be always at the same place.

[Originally posted on Stackoverflow: http://stackoverflow.com/questions/16018243/pylab-contour-plot-using-mollweide-projection-create-artefacts ]

Member

pelson commented Apr 16, 2013

The strip in the image suggests that there has been a polygon which has transformed badly. I'm 95% confident that this is because of your X limits are +/- Pi which are both singularities on the Mollweide projection.

Indeed, when I run your code (v1.3-dev), I get the following warning:

lib/matplotlib/projections/geo.py:484: RuntimeWarning: invalid value encountered in arcsin
  theta = np.arcsin(y / np.sqrt(2))

I've tried fiddling with the extents of the underlying data, but without much luck.

Have you considered using one of the mapping toolkits (Basemap or cartopy)?

@ghost

ghost commented Apr 16, 2013

I tried to restrict the range of the of X,Y. It does work for up to 99% of their boundary value. With 99.5% of (-pi/2, pi/2) and (-pi, pi) it shows even more artefacts.

The same code using Basemap works perfectly.

Member

dmcdougall commented Jun 16, 2013

@pelson Should the contouring code expect to handle post-contouring transforms to the data space? I think this is going to be a tough nut to crack.

Owner

mdboom commented Jun 17, 2013

@dmcdougall: I think the answer to your question is "would be nice", but not under the current implementation.

@tacaswell tacaswell added this to the v1.5.x milestone Aug 17, 2014

Owner

tacaswell commented Nov 26, 2014

This seems to have gotten worse:

test image

Member

WeatherGod commented Nov 27, 2014

This kinda looks like the polygon closing issue we fixed back in July for
mplot3d...

On Wed, Nov 26, 2014 at 3:11 PM, Thomas A Caswell notifications@github.com
wrote:

This seems to have gotten worse:

[image: test image]
https://cloud.githubusercontent.com/assets/199813/5208050/2c5c15ba-757e-11e4-9265-f9d680fc242c.png


Reply to this email directly or view it on GitHub
#1910 (comment)
.

Contributor

dstansby commented Nov 26, 2016

Looks a bit better now! This is on 2.0.0b4+2949.gf12abca

figure_1

Owner

tacaswell commented Nov 27, 2016

Fixed as far back as 1.5.0!

Might be interesting to track down when this was fixed (git bisect is fun!), but closing.

@tacaswell tacaswell closed this Nov 27, 2016

Owner

tacaswell commented Nov 27, 2016

so

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

plt.ion()

ra = np.linspace(-np.pi, np.pi, 40)
dec= np.linspace(-np.pi/2, np.pi/2, 20)
X,Y = np.meshgrid(ra,dec)
Z = np.sin(X) * np.cos(X) * np.sin(Y) * np.cos(Y)

plt.figure()
ax = plt.subplot(111, projection = 'mollweide')
ax.contourf(X,Y,Z,100)
ax.contour(X,Y,Z,10,colors='k')
ax.set_title(matplotlib.__version__)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment