python - 在不绘制先前点的情况下循环绘制 map

标签 python loops matplotlib

我正在尝试绘制漂浮在海中的点。以下代码有效,但也绘制了前面绘图的所有点:

Duration = 6 #hours

plt.figure(figsize=(20,10))#20,10
map = Basemap(width=300000,height=300000,projection='lcc',
        resolution='c',lat_0=51.25,lon_0=-4)#lat_0lon_0 is left under
map.drawmapboundary(fill_color='turquoise')
map.fillcontinents(color='white',lake_color='aqua')
map.drawcountries(linestyle='--')

x=[]
y=[]
for i in range (0,Duration):

    x,y = map(Xpos1[i],Ypos1[i])
    map.scatter(x, y, s=1, c='k', marker='o', label = 'Aurelia aurita', zorder=2)
    plt.savefig('GIF10P%d' %i)

Xpos1 和 Ypos1 是掩码数组列表。列表中的每个数组的长度为 10,因此每张图中应绘制 10 个点:

 Xpos1=[[latitude0,lat1,lat2,lat3,..., lat9],
       [latitude0,lat1,lat2,lat3,..., lat9],...]

这给了我六个数字,我会告诉你第一个和最后一个: First map Last picture

每张图片应该有 10 分,但最后一张是所有 map 的组合(所以 60 分)。 我如何仍然获得 6 张 map ,每张 map 只有 10 点?

编辑: 当我使用 matplotlib.pyplot will not forget previous plots - how can I flush/refresh? 的答案时我得到错误

ValueError: Can not reset the axes.  You are probably trying to re-use an artist in more than one Axes which is not supported 

当我使用来自 How to "clean the slate"? 的答案时,会弹出类似的错误 即,

plt.clf()
plt.cla()#after plt.show()

非常感谢任何帮助!

最佳答案

与其为每个图像绘制新的散点图,不如更新散点图的数据。优点是 map 只需要创建一次,节省了一些时间。

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

Duration = 6 #hours
Xpos1 = np.random.normal(-4, 0.6, size=(Duration,10))
Ypos1 = np.random.normal(51.25, 0.6, size=(Duration,10))

plt.figure(figsize=(20,10))
m = Basemap(width=300000,height=300000,projection='lcc',
        resolution='c',lat_0=51.25,lon_0=-4)
m.drawmapboundary(fill_color='turquoise')
m.fillcontinents(color='white',lake_color='aqua')
m.drawcountries(linestyle='--')

scatter = m.scatter([], [], s=10, c='k', marker='o', label = 'Aurelia aurita', zorder=2)

for i in range (0,Duration):
    x,y = m(Xpos1[i],Ypos1[i])
    scatter.set_offsets(np.c_[x,y])
    plt.savefig('GIF10P%d' %i)

plt.show()

关于python - 在不绘制先前点的情况下循环绘制 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50098388/

相关文章:

python - 如何强制 IPython 查看更新的库?

python - 如何预测 np.linalg.inv 的内存需求?

python - Windows XP 上的 VB6 COM 服务器与 Linux 上的 Python/D 之间通信的选项有哪些,不包括 .NET?

java - 如何输入字符作为输入以跳出寻找整数的循环?

python - 通过艺术家更新 matplotlib 中的文本

python - 如何不检查多个数据框 Pandas ?

java - Android Java While循环不执行

c++ - 循环检查约定

matplotlib - 如何使用 Pyplot 设置 Julia 中的刻度数?

animation - 如何设置动画的图像分辨率?