New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pcolormesh shifts grid cells to the east incorrectly #2181
Comments
Here is a MWE that works as I'd expect with no shifts, so I'm not following what you think needs updating. import cartopy.crs as ccrs
from matplotlib import pyplot as plt
import numpy as np
# get the data
lats = [-10, 0, 10]
lons = [-10, 0, 10]
lon_center = 0.0
map_proj = ccrs.Robinson(central_longitude=lon_center)
data_proj = ccrs.PlateCarree(central_longitude=lon_center)
fig = plt.figure()
ax = plt.axes(projection=map_proj)
ax.coastlines()
# ax.set_global()
ax.gridlines(xlocs=lons, ylocs=lats, linewidth=0.5, linestyle=(0,(5,2)), color='0.25')
ax.pcolormesh(lons, lats, np.arange(4).reshape((2, 2)), transform=data_proj)
sc = ax.scatter(lons, lats, c='k', marker='o', transform=data_proj)
plt.show() |
That page is saying that you need to specify lat/lon “edges” rather than “centres” to get what you want. You are already doing that. In any case that advice would now be out-of-date thanks to the shading options introduced in Matplotlib v3.3. So you should get something sensible regardless of whether you use “edges” or “centres”. |
OK, I think I've figured it out. The problem reported here does not apply to be because I'm already specifying the lat/lon edges. Rather, my problem comes from how
Now zoom into a small area at the intersection of (0,0), i.e., at the center of the plot, to minimize projection artifacts. Here is what I don't know if there's a fix to this, or if a fix is even needed. But given that I've found a way around, which is to not draw the edges, I'm happy to close this issue. |
Just to add some more context: I don't think this is Cartopy specific, but can likely be traced up to Matplotlib. I'm guessing you're getting something similar to this issue: matplotlib/matplotlib#17323 |
Description
I have a 2D array defined on a lat/lon grid that I can plot either with
contourf
orpcolormesh
. If I usecontourf
the colored boxes show up exactly where expected. However, if I usepcolormesh
the boxes are shifted to the east by a little bit. This is the same problem that's reported here. While that page offers a hacky fix, I'd much rather thatcartopy
did the right thing to begin with.Code to reproduce
I am including the snippets I believe are relevant, but I can include a MWE if that helps. The 2D array to be plotted is called
data
, so first define a set of latitude and longitude array edges forpcolormesh
Now plot using pcolormesh (
plot_ax
has been set up before, as hasdata_proj
andcmap
)This produces this map. There are two problems in this map:
data
array, except that it's not, which will become clear when compared againstcontourf
.To plot with
contourf
, I do the following:which produces this. Yes, there are some contouring artifacts, but neither of the above problems occur. Since my data are on a regular lat/lon grid, I'd really like to be able to just use
pcolormesh
and avoid contour calculations, but I can't because of the above problems.Operating system
MacOS Ventura 13.4
(all python packages, including python itself, installed with macports)
Cartopy version
0.21.0
Matplotlib version
3.5.3
The text was updated successfully, but these errors were encountered: