使用cartopy关键字’central_longitude()’时,出现不一致的海岸线()结果。

huangapple go评论64阅读模式
英文:

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. 使用cartopy关键字’central_longitude()’时,出现不一致的海岸线()结果。

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()

使用cartopy关键字’central_longitude()’时,出现不一致的海岸线()结果。

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. 使用cartopy关键字’central_longitude()’时,出现不一致的海岸线()结果。

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()

使用cartopy关键字’central_longitude()’时,出现不一致的海岸线()结果。

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!

huangapple
  • 本文由 发表于 2023年2月14日 01:43:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定