英文:
Inconsistent coastlines() results when using the cartopy keyword 'central_longitude()'
问题
I'm working with observational geospatial (ISCCP) data in python using xarray for handling and plotting. The data are on a 144-entry 2.5° grid beginning with 1.25°E, so longitude gridpoints are 1.25, 3.75, 6.25, etc., culminating at 358.75°E. When I plot other data, for example, MERRA-2 reanalysis data on a 2.5° grid using the central_longitude keyword in my projection like below, it properly centers both data and coastlines at the desired longitude.
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
merra = xr.open_dataset(...)
a = merra.OMEGA.plot.contourf(subplot_kws=dict(projection=
ccrs.PlateCarree(central_longitude=179.9999999999994),
transform=ccrs.PlateCarree()))
a.axes.coastlines()
The output to this looks like this, with certain important markers clearly visible that indicate the data was transformed correctly.
However, applying the same technique to the other data yields clearly incorrect results:
import matplotlib.pyplot as plt
import xarray as xr
import cartopy.crs as ccrs
isccp = xr.open_dataset(...)
a = isccp.lcf.mean(dim=['time', 'tau']).plot(subplot_kws=dict(projection=ccrs.PlateCarree(central_longitude=179.75),
transform=ccrs.PlateCarree()))
a.axes.coastlines()
You can clearly see the outline of data from Africa in what should be the central/eastern Pacific, and data from South America in the Maritime Continent.
Where is the problem arising? Is it in the way I'm applying the transform/projection? Could it be an import order issue? In rewriting my code for this post, I'm seeing that the two different python files have a different order in which I imported the libraries, so I included that in the above code blocks. I know that that sort of thing can lead to errors, especially when plotting using xarray is concerned.
英文:
I'm working with observational geospatial (ISCCP) data in python using xarray for handling and plotting. The data are on a 144-entry 2.5° grid beginning with 1.25°E, so longitude gridpoints are 1.25, 3.75, 6.25, etc., culminating at 358.75°E. When I plot other data, for example, MERRA-2 reanalysis data on a 2.5° grid using the central_longitude keyword in my projection like below, it properly centers both data and coastlines at the desired longitude.
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
merra = xr.open_dataset(...)
a = merra.OMEGA.plot.contourf(subplot_kws=dict(projection=
ccrs.PlateCarree(central_longitude=179.9999999999994),
transform=ccrs.PlateCarree()))
a.axes.coastlines()
The output to this looks like this, with certain important markers clearly visible that indicate the data was transformed correctly.
However, applying the same technique to the other data yields clearly incorrect results:
import matplotlib.pyplot as plt
import xarray as xr
import cartopy.crs as ccrs
isccp = xr.open_dataset(...)
a = isccp.lcf.mean(dim=['time', 'tau']).plot(subplot_kws=dict(projection=ccrs.PlateCarree(central_longitude=179.75),
transform=ccrs.PlateCarree()))
a.axes.coastlines()
You can clearly see the outline of data from Africa in what should be the central/eastern Pacific, and data from South America in the Maritime Continent.
Where is the problem arising? Is it in the way I'm applying the transform/projection? Could it be an import order issue? In rewriting my code for this post, I'm seeing that the two different python files have a different order in which I imported the libraries, so I included that in the above code blocks. I know that that sort of thing can lead to errors, especially when plotting using xarray is concerned.
答案1
得分: 0
I believe I've solved (?) the problem. Someone with more technical knowledge of the inner workings of how xarray/cartopy/matplotlib interact with each other might want to fact check me, but by first creating a subplots object with
subplot_kw=dict(projection=ccrs.PlateCarree(central_longitude=179.75),
transform=ccrs.PlateCarree())
in its keywords and then use
data.variable.plot.contourf(ax=[row,col],
projection=ccrs.PlateCarree(central_longitude=179.75),
transform=ccrs.PlateCarree())
to make each individual plot, I get correctly aligned data and coastlines. Probably arising as a result of something I'm doing wrong, but if not, hopefully this can help somebody!
英文:
I believe I've solved (?) the problem. Someone with more technical knowledge of the inner workings of how xarray/cartopy/matplotlib interact with eachother might want to fact check me, but by first creating a subplots object with
subplot_kw=dict(projection=ccrs.PlateCarree(central_longitude=179.75),
transform=ccrs.PlateCarree())
in its keywords and then use
data.variable.plot.contourf(ax=[row,col],
projection=ccrs.PlateCarree(central_longitude=179.75),
transform=ccrs.PlateCarree())
to make each individual plot, I get correctly aligned data and coastlines. Probably arising as a result of something I'm doing wrong, but if not, hopefully this can help somebody!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论