如何用 xarray 和 cartopy 使流线连续?

问题描述 投票:0回答:1

我现在正在使用 xarray 数据集在全球地图上用 cartopy 绘制流线图。然而,流线在跨越日期变更线时并不连续。有谁知道如何解决这个问题? 代码(只有streamplot部分)和输出图如下所示:

wnd = xr.merge([u[::2,::2],v[::2,::2]])
proj = ccrs.PlateCarree(central_longitude = 180)
fig, ax = plt.subplots(1,1,
                     subplot_kw={'projection': proj},
                     figsize=(12,7))
wind_plt = wnd.plot.streamplot(ax=ax,
                               transform=ccrs.PlateCarree(),
                               x='longitude', y='latitude',
                               u='u', v='v',
                               arrowsize=2,arrowstyle='->',
                               color="black",
                               )

ax.coastlines(color = 'dimgray')
ax.set_extent([0,360,-90,90],crs=ccrs.PlateCarree())
ax.set_xticks(np.arange(0, 361,90), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(-90,91,45), crs=ccrs.PlateCarree())  
ax.set_xlabel(' ')
ax.set_ylabel(' ')
ax.set_title(' ')
ax.text(70, 30, '03-12', fontsize = 14, transform=ccrs.PlateCarree())

ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.axis('off')

plt.savefig('0312.png', bbox_inches = 'tight', pad_inches = 0)
plt.close()

谢谢,欢迎大家提出意见!

python python-xarray cartopy
1个回答
0
投票

我怀疑您的流线没有创建到填充绘图区域所需的全部范围。或者,它们没有正确创建。

这是我的演示代码,它显示了完整的蒸汽图供您尝试。

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np

noproj = projection=ccrs.PlateCarree()
myproj = projection=ccrs.PlateCarree(central_longitude=180)

# Creating dataset
w = 180
h = 90
Y, X = np.mgrid[-h:h:100j, -w:w:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U**2 + V**2)

fig = plt.figure(figsize=[5,6.7])

# Plot on ordinary projection
ax1 = fig.add_subplot(211, projection = noproj)
ax1.streamplot(X, Y, U, V, density=[0.4, 0.8], transform=noproj)
ax1.set_extent([-180, 180, -90, 90], crs=noproj)
ax1.coastlines(resolution='110m', alpha=0.3)
ax1.gridlines(draw_labels=True)
ax1.set_title("Zero-meridian-centered")

# Plot on projection with shifted center
ax2 = fig.add_subplot(212, projection = myproj)
ax2.streamplot(X, Y, U, V, density=[0.4, 0.8], transform=noproj)
ax2.set_extent([-180, 180, -90, 90], crs=noproj)
ax2.coastlines(resolution='110m', alpha=0.3)
ax2.gridlines(draw_labels=True)
ax2.set_title("Dateline-centered")
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.