3

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. Every array in the lists has a length of 10, so 10 points should be plotted in each map:

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

This gives me six figures, I'll show you the first and last: First map Last picture

Every picture is supposed to have 10 points, but the last one is a combination of all the maps (so 60 points). How do I still get 6 maps with only 10 points each?

Edit: When I use the answer from matplotlib.pyplot will not forget previous plots - how can I flush/refresh? 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!

Jellyse
  • 839
  • 7
  • 22
  • Is only the last picture a combination of all the maps, or are all the pictures except the first one a combination of all the previous maps? – mkrieger1 Apr 30 '18 at 10:40
  • Also, please show where the `map` object (on which you call the `scatter` method) comes from. – mkrieger1 Apr 30 '18 at 10:42
  • @mkrieger1 the second is combination of 1 and 2, 3rd is a combination of 1,2,3 and so on. – Jellyse Apr 30 '18 at 10:45
  • 1
    `scatter` seems to add new points to the existing plot. You probably need to clear or recreate it each time after saving it. – mkrieger1 Apr 30 '18 at 10:47
  • Possible duplicate of [matplotlib.pyplot will not forget previous plots - how can I flush/refresh?](https://stackoverflow.com/questions/17106288/matplotlib-pyplot-will-not-forget-previous-plots-how-can-i-flush-refresh) – mkrieger1 Apr 30 '18 at 10:49
  • 1
    Try moving the contents outside the for loop into it on the top. – Primusa Apr 30 '18 at 11:25
  • @Primusa, that solved the problem! – Jellyse Apr 30 '18 at 11:30
  • Haha glad I could help :) – Primusa Apr 30 '18 at 11:31
  • 1
    As a small point `map` is a keyword - so avoid it for variables - that consufed me before you added extra details to the question – doctorlove Apr 30 '18 at 11:42

2 Answers2

3

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()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

As @Primusa said, simply moving all of the things into the for loop works to redefine the map. 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)

enter image description here

enter image description here

Jellyse
  • 839
  • 7
  • 22