contourf with lat-lon centers?
See original GitHub issueI have a set of data values that indicate the center of the grid boxes instead of the edges.
Using pcolormesh I can specify the lon, lat lists as the edges and it automatically interprets the data list as the centers (since it has one less value).
For example: len(lats) = 91, len(lons) = 181, len(data) = (90, 180)
basemap.pcolormesh(lons, lats, data, latlon=True)
This works fine and plots properly, however:
- pcolormesh is very slow; I would prefer to use contourf.
- pcolormesh does not provide interpolation so the basemap looks pixelated.
How can I create a contourf plot using the center of the grid boxes instead of the edges? If I change the lat, lon lists to be the centers instead of the edges to match the data list then I get missing data at the edge longitudes and latitudes.
Technically I could interpolate from grid box centers to grid box edges manually by averaging the neighboring centers to get the edges but I was hoping there was something out of the box I could use?
Or maybe there is some way to speed up pcolormesh and also apply some sort of smoothing/interpolation?
Code sample:
import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
lons = np.arange(-180.0, 180.0 + 1, 2)
lats = np.arange(-90.0, 90.0 + 1, 2)
lons, lats = np.meshgrid(lons, lats)
data = np.random.rand(90, 180)
# This works to plot center of grid boxes
bm = Basemap(projection='robin', lon_0=0, resolution='c')
bm.pcolormesh(lons, lats, data, latlon=True)
plt.show()
# contourf does not work to plot grid box centers
# bm = Basemap(projection='robin', lon_0=0, resolution='c')
# bm.contourf(lons, lats, data, latlon=True)
# plt.show()
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (6 by maintainers)
Oh, hmmm. Its possible basemap is mad about the lon at -181. So, just set to -179.9999
Pcolormesh and contourf are fundamentally different operations, even though they can be used for similar purposes. Pcolormesh fills quadrilaterals specified by their boundaries; contour draws lines based on values specified on a grid of points. It is up to the user to provide the appropriate grid for each.