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

时间:2018-04-30 10:14:43

标签: 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个点,但最后一个是所有地图的组合(所以60点)。 如何获得6张地图,每张只有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()

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

不是为每个图像绘制新的散点图,而是更新散点图的数据是有意义的。优点是只需创建一次地图,节省一些时间。

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

答案 1 :(得分:0)

正如@Primusa所说,只需将所有内容移动到for循环中,就可以重新定义地图。 那么正确的代码就是:

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)

enter image description here

enter image description here

相关问题