简体   繁体   English

在循环中绘制地图而不绘制先前的点

[英]Plot map in loop without plotting previous points

I am trying to plot points drifting in the sea. 我试图绘制漂浮在海中的点。 The following code works, but plots all the points of the previous plots as well: 以下代码有效,但也绘制了上一个图的所有点:

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 and Ypos1 are a list of masked arrays. Xpos1和Ypos1是掩码数组的列表。 Every array in the lists has a length of 10, so 10 points should be plotted in each map: 列表中的每个数组的长度都为10,因此每个映射中应绘制10个点:

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

This gives me six figures, I'll show you the first and last: 这给了我六个数字,我会告诉你第一个和最后一个: 第一张地图 最后一张照片

Every picture is supposed to have 10 points, but the last one is a combination of all the maps (so 60 points). 每张照片应该有10个点,但最后一个是所有地图的组合(所以60点)。 How do I still get 6 maps with only 10 points each? 我如何仍然获得6张地图,每张只有10分?

Edit: When I use the answer from matplotlib.pyplot will not forget previous plots - how can I flush/refresh? 编辑:当我使用来自matplotlib.pyplot的答案时不会忘记以前的情节 - 如何刷新/刷新? I get the error 我收到了错误

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

A similar error pops up when I use the answer from: How to "clean the slate"? 当我使用以下答案时会弹出类似的错误: 如何“清理平板”? namely, 即,

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

Any help is deeply appreciated! 任何帮助深表感谢!

Instead of plotting new scatter plots for each image it would make sense to update the scatter plot's data. 不是为每个图像绘制新的散点图,而是更新散点图的数据是有意义的。 The advantage is that the map only needs to be created once, saving some time. 优点是只需创建一次地图,节省一些时间。

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

As @Primusa said, simply moving all of the things into the for loop works to redefine the map. 正如@Primusa所说,只需将所有内容移动到for循环就可以重新定义地图。 The correct code is then: 那么正确的代码是:

for i in range (0,Duration,int(24/FramesPerDay)):
    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 = 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)

在此输入图像描述

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM