Repository navigation
Open
Labels
Milestone
Description
This example:
from metpy.plots import ImagePlot, MapPanel, PanelContainer
from metpy.units import units
import xarray as xr
ds_z500 = xr.open_dataset('/Users/rmay/Downloads/gfs.t00z.pgrb2.0p25.f024', engine='cfgrib', filter_by_keys={'typeOfLevel': 'isobaricInhPa', 'level': 500})
img = ImagePlot()
img.data = ds_z500
img.field = 't'
img.colormap = 'plasma'
panel = MapPanel()
panel.area = 'us'
panel.title = 'GFS 500mb Temp Forecast Example'
panel.plots = [img]
pc = PanelContainer()
pc.size = (10,8)
pc.panels = [panel]
pc.show()produces the following with 1.0 and main:

Taken from a Stack Overflow question. This pretty clearly relates to this code that wraps longitude:
MetPy/src/metpy/plots/declarative.py
Lines 1087 to 1102 in 6944e23
| @property | |
| def plotdata(self): | |
| """Return the data for plotting. | |
| The data array, x coordinates, and y coordinates. | |
| """ | |
| x = self.griddata.metpy.x | |
| y = self.griddata.metpy.y | |
| # At least currently imshow with cartopy does not like this | |
| if 'degree' in x.units: | |
| x = x.data | |
| x[x > 180] -= 360 | |
| return x, y, self.griddata |
because without it the same code gives:
The code is pretty clearly incorrect because it results in an x array where x[0] is 0 and x[-1] is -0.25, since this full dataset has a longitude range of [0, 360].
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dopplershift commentedon Jun 16, 2021
Of course it's not as simple as removing that bogus wrapping code, as that breaks a the
test_latlontest:produces

so that's lovely.
dopplershift commentedon Jun 16, 2021
Everything works fine if I change the extent of the image data (either with
extentmanually or in the data) from [250, 315] to [-110, -45 ]. It also behaves more as expected if the range is [179, 315].I've so far traced this into CartoPy's image warping code. It seems to be an issue where we're using the fact that
PlateCarreecan (sort of) handle longitudes in [0, 360], but when you transform points back intoPlateCarree, they're in [-180, 180]. I'll continue digging into CartoPy to try to figure out how to make it not completely broken (seeing as NCEP produces data that are natively [0, 360]).In the meanwhile, though, we need to unbreak the original example with MetPy, but also keep the test case working. This probably means writing some code to properly "roll" an image from [0, 360] to [-180, 180].
eliteuser26 commentedon Aug 10, 2021
I had to face this issue in the past where I had to wrestle with plotting with longitudes from -180 to 180 degrees. When plotting on a map with this set of longitudes it would warp the lines and wrap around the globe when taken from platecarree. The only solution that I found that prevented these problems was to convert the longitudes to 0 to 360 degrees and it works over the poles too. I tried both ways and looked at the results. It was quite convincing.
dopplershift commentedon Apr 27, 2022
Jotting down a thought here:
add_cyclic_point(or implement our own for dependency avoidance)