如何从酸洗的GeoAxesSubplot中提取数据以重新填充新图形?

2024-06-01 05:18:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想“下载”(保存在pickle文件中)一个加载在cartopy中的底图,这样我就可以在matplotlib{}中的两个轴中的一个轴上重新绘制,使用的脚本与我用来下载图像的脚本不同。我需要这样做,这样我就不会每次我需要一个基础层的时候都询问雄蕊

如何从p其中p==<cartopy.mpl.geoaxes.GeoAxesSubplot object at 0x124210e10>提取数据

脚本1

import matplotlib.pyplot as plt
import pickle
import cartopy.feature as cfeature
from cartopy.io.img_tiles import Stamen 

def main():
    tiler = Stamen('terrain-background')
    mercator = tiler.crs

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
    ax.set_extent([15.85583, 18.47056, 5.80167, 6.56194], crs=ccrs.PlateCarree())
    ax.add_image(tiler, 6)
    ax.coastlines('10m')
    #plt.show()

    pickle.dump(ax, open('map.pkl', 'wb'))

main()

脚本2

import matplotlib.pyplot as plt
import pickle

p=pickle.load(open('map.pkl', 'rb'))
#plt.show() #works; but I want to integrate as a 2nd axis in another figure, now its own figure 

fig, axes = plt.subplots(1, 2)
axes[1] = p
plt.show() #shows two axes; but axes[1] is not the map

注意:我标记了basemap包,因为cartopy在某个时候正在替换它


Tags: import脚本mapmatplotlibasshowfigplt
1条回答
网友
1楼 · 发布于 2024-06-01 05:18:28

在现有图形中,无法轻松地将一组轴交换到另一组轴

您可以在脚本2中尝试以下内容:

p = pickle.load(open('map.pkl', 'rb'))

fig = plt.figure()
p.figure = fig
fig.axes.append(p)
fig.add_axes(p)

定位加载的轴时可能会出现问题,您可以通过保存原始轴的位置,然后执行p.set_position(saved_position)来调整加载的轴

相关问题 更多 >