Вопрос

I have written a program in python to map the random positions of points on a disc for a project. The idea is that for different numbers of points the random positions will be saved as a png for use later. The code is as follows:

def randr():#random r position
  return random.random()

def randphi():#random phi position
  return 2*pi*random.random()

nrange=[3,10,11,12,15,20,25,30]

for N in nrange:
   rposition=[]#create a table of r positions
   phiposition=[]#create a table of phi positions
   rinverse=[]#create a table of 1/rij

   for listcreation in range(0,N,1):#go through each charge
      rposition.append(randr())#allocate a random r position
      phiposition.append(randphi())#allocate a random phi position

   name=N
   filename = "c:/users/V/%i.png" % name
   pyplot.polar(phiposition,rposition,marker='o', markersize=10,color='b',
             linestyle='none')
   pyplot.savefig(filename, format='png')

The problem is that when this code is run the first figure saves with 3 points but the second one saves with 13 instead of 10! This goes on for successive N

I cannot post images so you'll have to take my word for it or run the code.

Does anyone know how to overcome this?

Это было полезно?

Решение

By default, the axes are not cleared between plots, so when you plot the second plot, it adds to whatever was already plotted. To clear it, insert pyplot.cla() before your polar call.

Другие советы

I have found in similar situation it is good to properly initialise an axis that is

for N in nrange:
    {Make the data}

    ax = py.subplot(111)
    ax.plot({data})
    py.savefig...

This will automatically clear the axis and is clearly readable. If you also use py.xlabel you may wish to change these to ax.set_xlabel instead to make it clear.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top