basemap icon indicating copy to clipboard operation
basemap copied to clipboard

pcolormesh plot is blank; pcolor plot works

Open Timothy-W-Hilton opened this issue 10 years ago • 1 comments

I am experiencing strange behavior using mpl_toolkits.basemap.Basemap. My understanding is that pcolormesh is faster than pcolor, and thus preferable. Here is a minimal example (below) where I get a different plot from pcolormesh than from pcolor.

On two systems (mac os x 10.9.5; Ubuntu 11.04 (GNU/Linux 2.6.32.28 x86_64); basemap 1.0.7 on the mac and 1.0.8 on the Ubuntu machine; both using matplotlib 1.4.2) I get the expected grid of random colors from pcolor, but a monochrome plot from pcolormesh.

Is this expected? Are there circumstances in which I should be using pcolor instead of pcolormesh? Is this a bug in matplotlib or in basemap?

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

def setup_map(ax):

    m = Basemap(width=8.0e6,
                height=6.5e6,
                projection='aeqd',
                lat_0=54,
                lon_0=-105,
                resolution='l',
                area_thresh=1000, 
                rsphere=6371007.181000,
                fix_aspect=True,
                ax=ax)
    m.drawcoastlines()
    m.drawcountries()
    m.drawstates()
    return(m)

# create pseudo-data with longitudes and latitudes
lon, lat = np.meshgrid(np.arange(-180, 180), 
                       np.arange(90, -90, -1))
data = np.random.rand(*lon.shape) * 100

# two-panel figure
fig, ax = plt.subplots(nrows=1, ncols=2)

# plot pseudodata in left panel using pcolormesh
m0 = setup_map(ax[0])
cm = m0.pcolormesh(lon, lat, data, latlon=True, 
              vmin=0.0, vmax=100.0, cmap=plt.cm.get_cmap("Blues"))
plt.colorbar(cm, ax=ax[0])
ax[0].set_title('pcolormesh')

# plot pseudodata in left panel using pcolor
m1 = setup_map(ax[1])
cm = m1.pcolor(lon, lat, data, latlon=True, 
          vmin=0.0, vmax=100.0, cmap=plt.cm.get_cmap("Blues"))
plt.colorbar(cm, ax=ax[1])
ax[1].set_title('pcolor')

plt.show()

Timothy-W-Hilton avatar Dec 12 '14 17:12 Timothy-W-Hilton

Your having this problem because of your indexes; if you use

lon, lat = np.meshgrid(np.arange(-180, 180), np.arange(-90, 90, 1))

, reversing the second index, then the problem goes away. I suspect this is due to the way the mesh is constructed.

It might be useful to have the note about the index in the documentation.

davidmanheim avatar Dec 22 '14 21:12 davidmanheim