我正在使用cartopy
显示叠加在世界地图上的KDE。起初,我使用的是ccrs.PlateCarree
投影,没有任何问题,但当我尝试使用另一个投影时,它似乎爆炸了投影的规模。为了便于参考,我在下面提供了一个可以在自己的机器上测试的示例(只需注释掉两行projec
即可在投影之间切换)
from scipy.stats import gaussian_kde
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
projec = ccrs.PlateCarree()
#projec = ccrs.InterruptedGoodeHomolosine()
fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(projection=projec)
np.random.seed(1)
discrete_points = np.random.randint(0,10,size=(2,400))
kde = gaussian_kde(discrete_points)
x, y = discrete_points
# https://www.oreilly.com/library/view/python-data-science/9781491912126/ch04.html
resolution = 1
x_step = int((max(x)-min(x))/resolution)
y_step = int((max(y)-min(y))/resolution)
xgrid = np.linspace(min(x), max(x), x_step+1)
ygrid = np.linspace(min(y), max(y), y_step+1)
Xgrid, Ygrid = np.meshgrid(xgrid, ygrid)
Z = kde.evaluate(np.vstack([Xgrid.ravel(), Ygrid.ravel()]))
Zgrid = Z.reshape(Xgrid.shape)
ext = [min(x)*5, max(x)*5, min(y)*5, max(y)*5]
earth = plt.cm.gist_earth_r
ax.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '50m',
edgecolor='black', facecolor="none"))
ax.imshow(Zgrid,
origin='lower', aspect='auto',
extent=ext,
alpha=0.8,
cmap=earth, transform=projec)
ax.axis('on')
ax.get_xaxis().set_visible(True)
ax.get_yaxis().set_visible(True)
ax.set_xlim(-30, 90)
ax.set_ylim(-60, 60)
plt.show()
您会注意到,当使用ccrs.PlateCarree()
投影时,KDE被很好地放置在非洲上方,但是当使用ccrs.InterruptedGoodeHomolosine()
投影时,您根本看不到世界地图。这是因为世界地图的比例尺很大。下面是两个示例的图像:
中断的古德同色线投影(缩小):
如果有人能解释为什么会发生这种情况,以及如何修复它,这样我就可以在不同的预测上绘制相同的数据,那将不胜感激
编辑:
我还想具体说明,在我所包含的示例中,我尝试将transform=projec
添加到第37行,即:
ax.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '50m',
edgecolor='black', facecolor="none", transform=projec))
然而,这没有帮助。事实上,加上这一点,世界地图似乎不再出现
编辑:
作为对约翰回答的回应,这是我使用该代码时得到的图:
并缩小:
对您的绘图的评论:
绘图1:(参考地图)
Plot2
问:为什么地图上没有显示地形特征
答:该地块占地面积很小,不包括任何一块
Plot3
问:为什么地图上看不到Zgrid图像
答:绘图覆盖的区域非常大,以至于图像变得太小而无法绘图
补救措施(第2和第3部分)
关于“网格线”绘图
下面是运行并生成所需映射的修改代码
输出映射:
相关问题 更多 >
编程相关推荐