gpt4 book ai didi

python - 如何创建 Basemap 实例并使用不同的 "overlays"重用它?

转载 作者:行者123 更新时间:2023-12-01 02:58:22 24 4
gpt4 key购买 nike

我想创建一次 basemap 实例(因为它很昂贵),然后将其与不同的contourf()叠加层一起重用。

澄清一下,我需要( basemap +轮廓1)和( basemap +轮廓2),而不是( basemap +轮廓1+轮廓2),这就是this question大约是。

有例子herehere (来自同一作者的回复,相隔五年!),但我无法让一个简单的可重用代码片段工作。

据此second example ,我的代码结构如下:

%matplotlib inline
import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt


def get_basemap():
fig = plt.figure('Background')
fig.add_subplot(111)

map_ = Basemap(resolution='c', projection='stere', width=12000000,
height=8000000, lat_ts=50, lat_0=50, lon_0=260.)
map_.drawcoastlines()
map_.drawmapboundary()
map_.fillcontinents(zorder=-1)

fig.canvas.draw()
background = fig.canvas.copy_from_bbox(fig.bbox)

return map_, background


def plot_thing1(map_, background, lats, lons, data):
fig = plt.figure('Contourf', frameon=False)
fig.add_subplot(111, frameon=False)

fig.canvas.restore_region(background)

x, y = map_(*np.meshgrid(lons, lats))
map_.contourf(x, y, data, zorder=0)
fig.show()


def plot_thing2(map_, background, lats, lons, ugrd, vgrd):
fig = plt.figure('Quiver', frameon=False)
fig.add_subplot(111, frameon=False)

fig.canvas.restore_region(background)

x, y = map_(*np.meshgrid(lons, lats))
map_.quiver(x, y, ugrd, vgrd, zorder=0)
fig.show()


min_lat, max_lat = 20, 70
min_lon, max_lon = 210, 310
lats = np.arange(min_lat, max_lat)
lons = np.arange(min_lon, max_lon)
shape = len(lats), len(lons)

# Run this once to get a reusable basemap
map_, background = get_basemap()

# Random data for thing1
data = np.random.rand(*shape)
plot_thing1(map_, background, lats, lons, data)

# Random data for thing2
ugrd, vgrd = np.random.rand(*shape), np.random.rand(*shape)
plot_thing2(map_, background, lats, lons, ugrd, vgrd)

但是 plot_thing1() 会生成两个“图”,一个具有 basemap 背景,另一个具有轮廓本身(而不是在一个图中将轮廓叠加在 basemap 上)。并且 plot_thing2() 只生成箭袋。

如何让它们出现在同一个地 block 上? (并在 plot_thing1()plot_thing2() 等中重用 basemap )这可能与附加到错误的事件图形/子图/轴的事物有关,但我无法理解 Basemap() 如何适应 matplotlib 层次结构。

最佳答案

下面保存了三张图像,单独的背景、带有等高线图的背景和带有箭袋的背景:

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

def get_basemap():
fig = plt.figure('Background')
fig.add_subplot(111)
map_ = Basemap(resolution='c', projection='stere', width=12000000,
height=8000000, lat_ts=50, lat_0=50, lon_0=260.)
map_.drawcoastlines()
map_.drawmapboundary()
map_.fillcontinents(zorder=-1)
return map_

def plot_thing1(map_, lats, lons, data):
x, y = map_(*np.meshgrid(lons, lats))
c = map_.contourf(x, y, data, zorder=0)
return c

def plot_thing2(map_, lats, lons, ugrd, vgrd):
x, y = map_(*np.meshgrid(lons, lats))
q = map_.quiver(x, y, ugrd, vgrd, zorder=0)
return q

min_lat, max_lat = 20, 70
min_lon, max_lon = 210, 310
lats = np.arange(min_lat, max_lat)
lons = np.arange(min_lon, max_lon)
shape = len(lats), len(lons)

# Run this once to get a reusable basemap
m = get_basemap()
plt.savefig("background.png")
#Plot stuff on the axes
data = np.random.rand(*shape)
c = plot_thing1(m, lats, lons, data)
plt.savefig("thing1.png")
#remove the contourplot from the axes:
for member in c.collections:
member.remove()
#Plot new stuff on the axes
ugrd, vgrd = np.random.rand(*shape), np.random.rand(*shape)
q = plot_thing2(m, lats, lons, ugrd, vgrd)
plt.savefig("thing2.png")
# to remove the quiver use
# q.remove()
plt.show()

enter image description here

作为解决方法,要直接在 jupyter 笔记本中显示保存的图像,您可以执行以下操作

from IPython.display import Image, display

listOfImageNames = ['background.png','thing1.png', 'thing2.png']

for fn in listOfImageNames:
display(Image(filename=fn))

关于python - 如何创建 Basemap 实例并使用不同的 "overlays"重用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44028135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com