Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib extent with mollweide projection

I'm having some issues with matplotlib and projecting an array into a Mollweide projection. I'm using Python 3.6 and matplotlib 2.0.2

I have a np.array of shape (360, 180) and I cannot figure out how to get it to plot properly.

For example

fig = plt.figure()
ax = fig.add_subplot(111, projection='mollweide')
arr = np.zeros((360, 180))
extent=(-np.pi, np.pi, -np.pi/2., np.pi/2.)
im = ax.imshow(arr, cmap=plt.cm.jet,
     extent=extent, aspect=0.5)

Produces a blank image. Changing the extent to be in degrees:

fig = plt.figure()
ax = fig.add_subplot(111, projection='mollweide')
arr = np.zeros((360, 180))
extent=(-180, 180, -180/2., 180/2.)
im = ax.imshow(arr, cmap=plt.cm.jet,
     extent=extent, aspect=0.5)

Produces this image Matplotlib image. Any help would be really appreciated.

like image 247
Joe Avatar asked Oct 27 '25 04:10

Joe


1 Answers

Showing an image with plt.imshow on a non-cartesian projection will mostly fail. What is possible however is to use a pcolormesh. The mollweide projection would require the coordinates in the range -π,π and -π/2., π/2. You can hence create a respective grid with numpy.meshgrid and plot the array on it with pcolormesh. (Note that the y/latitude is the first dimension of that array.)

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='mollweide')
arr = np.random.rand(180, 360)

lon = np.linspace(-np.pi, np.pi,360)
lat = np.linspace(-np.pi/2., np.pi/2.,180)
Lon,Lat = np.meshgrid(lon,lat)

im = ax.pcolormesh(Lon,Lat,arr, cmap=plt.cm.jet)

plt.show()

enter image description here

like image 91
ImportanceOfBeingErnest Avatar answered Oct 29 '25 17:10

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!