Question

The purpose of the script is to do a contour plot of some data in a Mollweide projection with pylab. There is a strange behaviour of contourf. Here is the 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: enter image description here

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

Is there a way to correct this artefacts generation ?

Was it helpful?

Solution

It seems that there is some underlying issue due to the fact that the Mollweide projection has singularities for latitudes of +/- 90°. A solution suggested by @pelson on github is to use Basemap:

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

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)
RAD = 180/np.pi

m = Basemap(projection='moll',lon_0=0,resolution='c')
m.contour(X*RAD, Y*RAD, Z, 10, colors='k',latlon=True)
m.contourf(X*RAD, Y*RAD, Z, 100, cmap=plt.cm.jet,latlon=True)

plt.show()

This code works as proven below: enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top