[Tutor] How to plot amount of lines but the amount is a input

Tom Xu xupeng.psu at gmail.com
Wed Dec 30 00:55:43 EST 2015


Dear Pythoneers,

I want to draw several curves in one graph, but the amount of curves is not
a fixed number.
My homework is below:
---
import matplotlib.pyplot as plt
import math

if __name__=='__main__':
    try:
        n=int(input('how many curves? '))
        ulist=[]
        thetalist=[]
        for i in range(n):
            ulist.append(float(input('Enter velocity: ')))
            thetalist.append(float(input('Enter angle: ')))
    except ValueError:
        print('invalid input')
    else:
        generateFr(?????)

def generateFr(u, theta):
    theta=math.radians(theta)
    g=9.8

    tflight=2*u*math.sin(theta)/g
    intervals=frange(0, tflight, 0.001)

    x=[]
    y=[]
    for t in intervals:
        x.append(u*math.cos(theta)*t)
        y.append(u*math.sin(theta)*t - 0.5*g*t*t)
    drawgraph(x, y)

def frange(start, final, increment):
    numbers=[]
    while start<final:
        numbers.append(start)
        start+=increment
    return numbers

def drawgraph(x, y):
    plt.plot(x, y)
    plt.xlabel('x-coordinate')
    plt.ylabel('y-coordinate')
    plt.title('Projectile motion')


More information about the Tutor mailing list