将cartopy地图旋转90度

发布于 2025-01-14 02:18:02 字数 2095 浏览 4 评论 0原文

我正在尝试使用 cartopy 创建地图,但将该地图旋转 90 度(请参阅下图,了解我正在尝试执行的操作的粗略草图*)。 这是我想做的事情的粗略草图这是使用以下代码生成的:

import xarray as xr
from cartopy import crs as ccrs
from matplotlib import pyplot as plt

ds = xr.open_dataset('http://iridl.ldeo.columbia.edu/SOURCES/.CAC/climatology+.sst/dods',
                decode_times=False)
ax = plt.subplot(projection=ccrs.PlateCarree())
ds.sel(X=slice(130,150)).mean('T').sst.plot(transform=ccrs.PlateCarree())
ax.coastlines()

现在,感谢 这个答案我知道我可以轻松实现这一目标,而无需使用matplotlib转换进入cartopy

import xarray as xr
from matplotlib import pyplot as plt
from matplotlib import transforms

ds = xr.open_dataset('http://iridl.ldeo.columbia.edu/SOURCES/.CAC/climatology+.sst/dods',
                decode_times=False)

ax = plt.subplot()
base = ax.transData
rot = transforms.Affine2D().rotate_deg(270)
plt.pcolormesh(ds.sel(X=slice(130,150)).X,
               ds.Y,
               ds.sel(X=slice(130,150)).mean('T').sst,transform= rot + base)
ax.set_aspect('equal')

它会产生: 输入图片此处描述(当然,由于变换,经度标签被关闭,但无论如何我都会删除轴标签)

但是,我想绘制海岸线(和国家边界,来自自然地球),这是人们可以做的事情使用cartopy的内置函数,但它与Affine2D()转换不兼容(并且可能与matplotlib的内置变换 一般的)。

那么 - 基本上,有没有一种方法可以生成旋转的图形,而又不会失去对 cartopy 的海岸线和边界绘制方法的访问权限?我可以想象

  • 我丢失了 ccrs.PlateCarree() 的一些输入?
  • 有什么方法可以在 matplotlib 中旋转整个子图而不触及其内容?

我很感激任何帮助!

*(基本上,我显示的是海表温度 - 不是使用此数据,这只是可轻松下载的示例数据 - 地球的子午线切片,并将在上面的垂直 x 纬度子图中显示该子午线切片上方的其他大气变量,具有匹配的纬度,所以我需要地图是水平的)

更新:

自己下载国家边界/海岸线的shapefile(例如,从NaturalEarth,而不是使用cartopy的内置基本shapefile管理系统)允许使用与以前相同的转换将它们输入到图中。

这仍然没有完全达到我想要的(对旋转图形的完整 cartopy crs 支持),但更接近了。

I'm trying to create a map using cartopy, but have that map be rotated 90 degrees (see image below for a rough sketch of what I'm trying to do*).
Here's a rough sketch of what I want to do
which was produced using the following code:

import xarray as xr
from cartopy import crs as ccrs
from matplotlib import pyplot as plt

ds = xr.open_dataset('http://iridl.ldeo.columbia.edu/SOURCES/.CAC/climatology+.sst/dods',
                decode_times=False)
ax = plt.subplot(projection=ccrs.PlateCarree())
ds.sel(X=slice(130,150)).mean('T').sst.plot(transform=ccrs.PlateCarree())
ax.coastlines()

Now, thanks to this answer I know that I can achieve this easily without going into cartopy using matplotlib transforms:

import xarray as xr
from matplotlib import pyplot as plt
from matplotlib import transforms

ds = xr.open_dataset('http://iridl.ldeo.columbia.edu/SOURCES/.CAC/climatology+.sst/dods',
                decode_times=False)

ax = plt.subplot()
base = ax.transData
rot = transforms.Affine2D().rotate_deg(270)
plt.pcolormesh(ds.sel(X=slice(130,150)).X,
               ds.Y,
               ds.sel(X=slice(130,150)).mean('T').sst,transform= rot + base)
ax.set_aspect('equal')

which produces:
enter image description here
(and sure, the longitude labels are off because of the transform, but I'd have deleted the axis labels anyways)

However, I would like to plot coastlines (and country borders, from natural earth), which is something that one can do using cartopy's built-in functions, but which isn't compatible with the Affine2D() transform (and, presumably, matplotlib's built-in transforms in general).

So - basically, is there a way to produce that rotated figure, without losing access to cartopy's coastlines and border plotting methods? I could imagine either

  • some input to ccrs.PlateCarree() that I'm missing?
  • some way to rotate the entire subplot in matplotlib without touching its contents at all?

I appreciate any help!

*(basically, I'm showing SSTs - not with this data, this is just sample data that's easily downloadable - of a meridional slice of the globe, and will be showing other atmospheric variables above that meridional slice in a vertical x latitude subplot above that, with matching latitudes, so I need the map to be horizontal)

Update:

Downloading shapefiles for country borders / coastlines myself (e.g., from NaturalEarth, instead of using cartopy's built-in basic shapefile management system) allows them to be inputted into the figure using the same transform as before.

This still doesn't quite get what I want (full cartopy crs support for the rotated figure) but is a lot closer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文