【发布时间】:2018-02-27 22:55:00
【问题描述】:
我是底图和 python 的新手,但我正在尝试为日常 cron 作业的天气模型构建绘图仪。我们每天绘制大约 1000 张图像。
我写了一些脚本来实现我想要的。但它花了很长时间,因为它为每个时间步重新绘制底图。绘制底图用了30秒,绘制contourf()只用了4秒。
我有一些想法可以通过预先绘制底图并在每次迭代中更新 contourf() 来加快进程。但我不明白 matplotlib 对象是如何工作的。
我已经研究过有关此问题的相同问题,但没有发现任何问题。但我从 user3982706 的 here 中发现了类似的东西。
from matplotlib import animation
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
fig, ax = plt.subplots()
# set up map projection
m = Basemap(projection='nsper',lon_0=-0,lat_0=90)
m.drawcoastlines()
m.drawparallels(np.arange(0.,180.,30.))
m.drawmeridians(np.arange(0.,360.,60.))
# some 2D geo arrays to plot (time,lat,lon)
data = np.random.random_sample((20,90,360))
lat = np.arange(len(data[0,:,0]))
lon = np.arange(len(data[0,0,:]))
lons,lats = np.meshgrid(lon,lat)
# ims is a list of lists, each row is a list of artists to draw in the
# current frame; here we are animating three artists, the contour and 2
# annotatons (title), in each frame
ims = []
for i in range(len(data[:,0,0])):
im = m.contourf(lons,lats,data[i,:,:],latlon=True)
add_arts = im.collections
text = 'title={0!r}'.format(i)
te = ax.text(90, 90, text)
an = ax.annotate(text, xy=(0.45, 1.05), xycoords='axes fraction')
ims.append(add_arts + [te,an])
ani = animation.ArtistAnimation(fig, ims)
## If you have ffmpeg you can save the animation by uncommenting
## the following 2 lines
# FFwriter = animation.FFMpegWriter()
# ani.save('basic_animation.mp4', writer = FFwriter)
plt.show()
该脚本将轮廓数据保存为艺术家列表。我不需要动画。所以我需要编辑这个脚本以将图形保存在循环中。所以这个脚本产生了figure1.png、figure2.png、figure3.png等等。
有什么建议吗?
【问题讨论】:
-
如果您是新手并且尚未依赖 Basemap 中的现有工作,那么我强烈建议您使用 cartopy 而不是 basemap。它可以为您省去很多麻烦。
标签: python matplotlib matplotlib-basemap