import matplotlib.pyplot as plt def populations(prey0,pred0,growth,predation,conversion,death,years): """Returns the predicted populations of two species, given their initial populations, the prey's growth rate, the predation rate, the predator's food conversion rate, the predator's death rate, and the number of years to predict.""" prey = [prey0] pred = [pred0] for y in range(years): prey.append(prey[y] + prey[y] * (growth-predation*pred[y])) pred.append(pred[y] + pred[y] * (conversion*prey[y]-death)) return prey, pred def plotPopulations(times,prey,pred,preyname,predname): """Displays a plot of two populations over the given times.""" # Prey use circles connected by solid line. preyPlot = plt.plot(times, prey, 'o-' ) # Predators use squares connected by dotted line. predPlot = plt.plot(times, pred, 's:' ) # Place legend box in "best" location. plt.legend( (preyPlot, predPlot), (preyname, predname), 'best') plt.xlabel('Years') plt.ylabel('Population') plt.title('Predator-Prey Model') plt.show() prey, pred = populations(100,50,.4,.003,.004,.2,10) plotPopulations(range(len(prey)),prey,pred,"Hare","Lynx")