question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

contourf with lat-lon centers?

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jklymakcommented, May 30, 2018

Oh, hmmm. Its possible basemap is mad about the lon at -181. So, just set to -179.9999

0reactions
efiringcommented, May 31, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Aligning data (contourf) on Basemap - python - Stack Overflow
Once I change the center location, the map moves but the data doesn't. I would be grateful for advice. I've created a simple...
Read more >
Ploting Lat Long and Frequency in a contourf map - MathWorks
I am currently trying to create a contourf plot of my data that I collected in the field for my dissertation. I am...
Read more >
Making Maps using Cartopy — Pangeo-at-AOES 0.1.1 ...
Making Maps using Cartopy ¶. Cartopy is a Python map plotting package. Combined with matplotlib is works well for making contour plots of...
Read more >
Function contourfm2
CONTOURFM Filled contour map. CONTOURFM(lat,lon,map) produces a contour plot of map data projected onto the current map axes.
Read more >
Plotting data — Basemap tutorial 0.1 documentation
x and y give the positions of the grid data if the latlon argument is true, the values ... containing a list of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found