Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用basemap和matplotlib打印timeseries等高线数据_Python_Matplotlib_Matplotlib Basemap - Fatal编程技术网

Python 使用basemap和matplotlib打印timeseries等高线数据

Python 使用basemap和matplotlib打印timeseries等高线数据,python,matplotlib,matplotlib-basemap,Python,Matplotlib,Matplotlib Basemap,我不熟悉basemap和python,但我正在尝试为日常cron工作构建天气模型绘图仪。我们每天绘制大约1000幅图像 我写了一些脚本来实现我想要的。但是它花费了很长时间,因为它重新绘制了每个时间步的底图。绘制底图需要30秒,绘制轮廓图只需要4秒() 我想通过在每次迭代中预先绘制底图并更新contourf()来加快过程。但我不明白matplotlib对象是如何工作的 我也研究过这个问题,但没有发现任何问题。但我从用户3982706中发现了类似的东西 from matplotlib import

我不熟悉basemap和python,但我正在尝试为日常cron工作构建天气模型绘图仪。我们每天绘制大约1000幅图像

我写了一些脚本来实现我想要的。但是它花费了很长时间,因为它重新绘制了每个时间步的底图。绘制底图需要30秒,绘制轮廓图只需要4秒()

我想通过在每次迭代中预先绘制底图并更新contourf()来加快过程。但我不明白matplotlib对象是如何工作的

我也研究过这个问题,但没有发现任何问题。但我从用户3982706中发现了类似的东西

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等等

有什么建议吗?

(a)使用动画保存图像 由于代码中已经存在动画,因此可以通过
ani.save
命令直接保存动画的每个图像

ani.save("figure.png", writer="imagemagick")
这将创建20个图形,称为
figure-0.png
figure-19.png
。它要求安装imagemagick并在您的环境中可用

(b) 保存个人数字 如果上述操作因任何原因失败,您可以通过
plt.savefig()
保存单个图形。在每个循环步骤结束时,将从轴中删除美工人员并将其删除

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

fig, ax = plt.subplots()

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.))

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)

for i in range(len(data[:,0,0])):
    im = m.contourf(lons,lats,data[i,:,:],latlon=True)
    text = 'title={0!r}'.format(i)
    te = ax.text(90, 90, text)
    an = ax.annotate(text, xy=(0.45, 1.05), xycoords='axes fraction')
    # save the figure
    plt.savefig("figure_{}.png".format(i))
    # remove stuff from axes
    for c in im.collections:
        c.remove()
    te.remove()
    an.remove()
    del im; del te; del an

plt.show()
(a) 使用动画保存图像 由于代码中已经存在动画,因此可以通过
ani.save
命令直接保存动画的每个图像

ani.save("figure.png", writer="imagemagick")
这将创建20个图形,称为
figure-0.png
figure-19.png
。它要求安装imagemagick并在您的环境中可用

(b) 保存个人数字 如果上述操作因任何原因失败,您可以通过
plt.savefig()
保存单个图形。在每个循环步骤结束时,将从轴中删除美工人员并将其删除

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

fig, ax = plt.subplots()

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.))

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)

for i in range(len(data[:,0,0])):
    im = m.contourf(lons,lats,data[i,:,:],latlon=True)
    text = 'title={0!r}'.format(i)
    te = ax.text(90, 90, text)
    an = ax.annotate(text, xy=(0.45, 1.05), xycoords='axes fraction')
    # save the figure
    plt.savefig("figure_{}.png".format(i))
    # remove stuff from axes
    for c in im.collections:
        c.remove()
    te.remove()
    an.remove()
    del im; del te; del an

plt.show()

如果您是新手,并且还不依赖Basemap中现有的工作,那么我强烈建议您使用cartopy而不是Basemap。如果你是新手并且还不依赖于Basemap中已有的工作,那么我强烈建议你使用cartopy而不是Basemap。它可以帮你省去很多麻烦。谢谢你的帮助。但是,当我尝试这一点时,代码运行没有错误。并且只生成了一个大文件大小的“figure.png”。而且它不能打开。我正在使用windows、matplotlib 2.0.2和imagemagick安装程序,我不知道出了什么问题,它对我来说很好。所以我添加了一个不需要动画的替代品。谢谢你的替代品!它起作用了!我真傻。我不知道contourf对象可以通过从集合中移除来移除。我认为它不能被删除,因为它没有删除方法。先生,我能再问你一个问题吗?我是否可以保存basemap状态,以便以后不用重新绘制就可以使用它?谢谢总是要画一个数字。因此,您不能跳过重新绘制的步骤,但在不重新绘制时,您可以节省一些时间。Matplotlib数字。如果涉及basemap,我不确定它是否有效,但肯定值得一试。谢谢你的帮助。但是,当我尝试这一点时,代码运行没有错误。并且只生成了一个大文件大小的“figure.png”。而且它不能打开。我正在使用windows、matplotlib 2.0.2和imagemagick安装程序,我不知道出了什么问题,它对我来说很好。所以我添加了一个不需要动画的替代品。谢谢你的替代品!它起作用了!我真傻。我不知道contourf对象可以通过从集合中移除来移除。我认为它不能被删除,因为它没有删除方法。先生,我能再问你一个问题吗?我是否可以保存basemap状态,以便以后不用重新绘制就可以使用它?谢谢总是要画一个数字。因此,您不能跳过重新绘制的步骤,但在不重新绘制时,您可以节省一些时间。Matplotlib数字。如果涉及basemap,我不确定它是否有效,但肯定值得一试。