[Tutor] How to plot amount of lines but the amount is a input
Alan Gauld
alan.gauld at btinternet.com
Wed Dec 30 06:35:19 EST 2015
On 30/12/15 05:55, Tom Xu wrote:
> Dear Pythoneers,
>
> I want to draw several curves in one graph, but the amount of curves is not
> a fixed number.
OK, But what exactly are you asking us to do?
> 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(?????)
You need to put the call to generateFr inside a loop that
iterates n times putting successive values from ulist and
thetalist into the function.
> 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')
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list