0

I work on a crowd simulation, and I tried to get a simple representation at a given time like this : new to the site so here is the link. I work with Spyder and the code works wonderfully when I display the image in ipython with plt.show(), but when i try to save the images with plt.savefig() (I removed plt.show() prior to that, not the issue) i ended up with blank images. Here is the code :

p,v,t = resolve() #p[c][i] is the position vector of individual i at time t[c]
N = len(t) # number of instant
n = len(m) # number of individual
murs_x = [w[0] for w in W] # wall points x coordinates
murs_y = [w[1] for w in W] # wall points y coordinates
conv = 39.3701 #inch/m
L = longueur*conv/50 # width of figure
H = (largeur + decalage)*conv/50 # height of figure

for c in range(N):
    
    fig1 = plt.figure(num="aff",figsize = (L,H), dpi = 200) # arbitrary num, allow to recreate the figure
    ax = fig1.add_axes([1,1,1,1])
    ax.set_axis_off() # visual purpose
    ax.set_frame_on(False) # visual purpose
    ax.axis([0,longueur,0,largeur+decalage])
    ax.scatter(murs_x,murs_y,s=0.01,marker='.')
    for i in range(n):
        if p[c][i][1] <= (largeur + r[i]): # presence condition for individual i
            ax.add_artist(plt.Circle((p[c][i][0], p[c][i][1]), r[i], alpha=1))
            # drawing of the circle representing individual i
    # here is the plt.show(), unused
    fig1.savefig(str(c)+".png") # trying to save instant c visual represention 
    fig1.clf()

Moreover, without the 2 lines for visual purposes, the images are not totally blank but rather like this : another link. I first attempted to use matplotlib.animation to create a video, however i had the same issue of a blank video with 2 cropped zeros in the upper right corner. I suppose that the issue is linked to the artist class (I had better results using scattered points instead of circles to represent each individual) but I am a beginner and do not know how to handle it precisely. At least the size of the image is the one expected one.

Thanks for reading this.

Ityl
  • 1
  • One problem is that matplotlib doesn't automatically updates the limits. You can either set them manually (`ax.set_xlim(xmin, xmax)` and `ax.set_ylim(ymin, ymax)`), or use `ax.relim()`and `ax.autoscale_view()` ([example code](https://matplotlib.org/stable/gallery/misc/packed_bubbles.html#sphx-glr-gallery-misc-packed-bubbles-py)). Another problem are the strange parameters to `add_axes` which places the axes outside the figure. See [here](https://stackoverflow.com/questions/43326680/what-are-the-differences-between-add-axes-and-add-subplot) for a description. – JohanC May 18 '21 at 18:53
  • Thanks for your answer. I did not understand how .add_axes work as the documentation says "All quantities are in fractions of figure width and height", I misunderstood what the first two parameters were. Thanks a lot for your explainations. – Ityl May 19 '21 at 11:46

0 Answers0