2

Why would matplotlib's contour and contourf produce inconsistent results?

I have some data that I wish to plot on a map using the matplotlib basemap toolkit's stereographic projection. When I plot the data with filled contours, contourf, I see the first picture below. Note the red (positive values) over most of North America. When I change contourf to contour, making no other changes, I see the second picture below. Note the streak of blue (negative values) over central North America.

The contour result with the blue is what is actually in the data, and therefore what I expect to see. The contourf result is not what I expect. What could cause this?

enter image description hereenter image description here

The python code is reproduced below. Here data is a float32 ndarray of size (73,144), lon is a float32 ndarray of size (144,), and lat is a float32 ndarray of size (73,). data is some NCEP temperature anomaly data evaluated on a 2.5 degree latitude/longitude grid.

import numpy as np
import matplotlib.pyplot as plt

# ...read data, lon, and lat from file here...

m = Basemap(width=10000000,height=6000000,
        resolution='l',projection='stere',\
        lat_ts=50,lat_0=50,lon_0=253)
m.drawcoastlines()
lon2d, lat2d = np.meshgrid(lon,lat)
x, y = m(lon2d,lat2d)
mymap = plt.contourf(x,y,data,levels=np.arange(17)-8,cmap=plt.cm.bwr)
plt.colorbar(mymap,orientation='vertical',shrink=0.75)
plt.show()

Update: The cylindrical plot produces what I expect with filled contours: enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.