pyplot and python. saving multiple graphs
<p>Well, we were recently given a project to make a natural selection simulator that tracks down the frequency of alleles through generations given particular particular viability of each allele pair. I made a program in python to print the graphs and it's working quite fine.</p>

<p>Here it is.</p>

<pre><code>#!/usr/bin/env python
import matplotlib.pyplot as plt
print ("Number of generations:")
n=int(input())
genp=[]
genq=[]

print ("What is p:")
p=float(input())
genp.append(p)
toap=1-p
genq.append(toap)

print ("What is w11:")
w11=float(input())
print ("What is w12:")
w12=float(input())
print ("What is w22:")
w22=float(input())

for i in range(1,n):
    genp.append((genp[i-1]*(genp[i-1]*w11+genq[i-1]*w12))/(genp[i-1]*genp[i-1]*w11+2*genp[i-1]*genq[i-1]*w12+genq[i-1]*genq[i-1]*w22))
    genq.append(1-genp[i])

plt.plot(range(0,n),genp,'r-',label='A')
plt.plot(range(0,n),genq,'b--',label='a')
plt.xlabel('Generations')
plt.ylabel('Allele Frequency')
plt.legend(loc='upper left')

plt.axis([0, n, -0.01, 1.01])
plt.title('p='+str(p)+' w11='+str(w11)+' w12='+str(w12)+' w22='+str(w22)+' for '+str(n)+' generations.', fontsize=14, fontweight='bold')
plt.savefig('graphP-'+str(p)+'w11-'+str(w11)+'w12-'+str(w12)+'w22-'+str(w22)+'.png')

plt.show()
</code></pre>

<p>Now,I decided to be a little lazy and make the program do the work of iterating through different values of viabilities. </p>

<p>Here's how I modified it.</p>

<pre><code>#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
print ("Number of generations:")
n=int(input())

genp=[]
genq=[]

print ("What is p:")
p=float(input())
genp.append(p)
toap=1-p
genq.append(toap)

for l in range(1,10):
    for j in range(1,10):
        for k in range(1,10):

            w11=l*0.1

            w12=j*0.1

            w22=k*0.1

            for i in range(1,n):
                genp.append((genp[i-1]*(genp[i-1]*w11+genq[i-1]*w12))/(genp[i-1]*genp[i-1]*w11+2*genp[i-1]*genq[i-1]*w12+genq[i-1]*genq[i-1]*w22))
                genq.append(1-genp[i])

            plt.plot(range(0,n),genp,'r-',label='A')
            plt.plot(range(0,n),genq,'b--',label='a')
            plt.xlabel('Generations')
            plt.ylabel('Allele Frequency')
            plt.legend(loc='upper left')

            plt.axis([0, n, -0.01, 1.01])
            plt.title('p='+str(p)+' w11='+str(w11)+' w12='+str(w12)+' w22='+str(w22)+' for '+str(n)+' generations.', fontsize=14, fontweight='bold')
            plt.savefig('graphP-'+str(p)+'w11-'+str(w11)+'w12-'+str(w12)+'w22-'+str(w22)+'.png')
</code></pre>

<p>I don't know why but executing this program is leading to errors.</p>

<pre><code>Traceback (most recent call last):
  File "./iterative.py", line 30, in &lt;module&gt;
    plt.plot(range(0,n),genp,'r-',label='A')
  File "/usr/lib/python3.5/site-packages/matplotlib/pyplot.py", line 3153, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/lib/python3.5/site-packages/matplotlib/__init__.py", line 1819, in inner
    return func(ax, *args, **kwargs)
  File "/usr/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 1382, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/usr/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 381, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "/usr/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 359, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "/usr/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 219, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
</code></pre>

<p>I searched stack overflow for the last error and found that it arises when the number of elements in x and y arrays is not same. The first program however is not giving this error.</p>

<p>I may not be fully sure what you are trying, and it might have been a good idea to change user inputs against fixed values in your program, since nobody knows the vaules for your input parameters.</p>

<p>Nevertheless I think your error is related to not initializing genp and genq properly for each loop cycle.</p>

<p>So I have made 2 changes:</p>

<ul>
<li>I moved the initialization into the loop and  </li>
<li>I cleared genp and genq after each loop cycle</li>
</ul>

<p>Hope that solves your issues.</p>

<p><strong>Hint:</strong> A way to track down your error would have been to analyze the actual dimensions of x and y at the line where the error occured, using a command like <code>print len(x)</code></p>

<pre><code>#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
print ("Number of generations:")
n=int(input())

genp=[]
genq=[]

print ("What is p:")
p=float(input())


for l in range(1,10):
    for j in range(1,10):
        for k in range(1,10):

            # Move this part of code into your loop for initialization
            genp.append(p)
            toap=1-p
            genq.append(toap)            
            # end

            w11=l*0.1

            w12=j*0.1

            w22=k*0.1

            for i in range(1,n):
                genp.append((genp[i-1]*(genp[i-1]*w11+genq[i-1]*w12))/(genp[i-1]*genp[i-1]*w11+2*genp[i-1]*genq[i-1]*w12+genq[i-1]*genq[i-1]*w22))
                genq.append(1-genp[i])


            plt.plot(range(0,n),genp,'r-',label='A')
            plt.plot(range(0,n),genq,'b--',label='a')
            plt.xlabel('Generations')
            plt.ylabel('Allele Frequency')
            plt.legend(loc='upper left')

            plt.axis([0, n, -0.01, 1.01])
            plt.title('p='+str(p)+' w11='+str(w11)+' w12='+str(w12)+' w22='+str(w22)+' for '+str(n)+' generations.', fontsize=14, fontweight='bold')
            plt.savefig('graphP-'+str(p)+'w11-'+str(w11)+'w12-'+str(w12)+'w22-'+str(w22)+'.png')

            #clear plot
            plt.clf()                

            # Empty ypur lists after each run
            genp=[]
            genq=[]            
            #end
</code></pre>

python matplotlib graph