Plotting a function in Python 2.7
Question: I am trying to plot f in this program but I am screwing something up. Can
someone have a look and inform me as to where I am messing up. Thanks.

    
    
    import math
    #x is the horizontal distance that the ball has traveled
    g=9.81
    v=raw_input('Enter an initial velocity:')
    theta=raw_input('Enter the angle that the object was thrown at:')
    y=raw_input('Enter the initial position of the object on the y-axis:')
    t=(2*v*math.sin(theta))/g
    x=(0.5)*((v*math.sin(theta))+v)*t
    float(v)
    float(theta)
    float(y)
    float(t)
    f=x*math.tan(theta)-(1/(2*(v**2)))*((g(x**2))/(math.cos(theta)**2))+y
    figure(1)
    clf()
    
    plot(f)
    xlabel('x')
    ylabel('y')
    show()
    


Answer: So first of all, I would import numpy and matplotlib

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    

Then, you have to convert your string input into floats, for that you can use
eval.

    
    
    initial_velo = eval(raw_input("Whatever you like: "))
    ...
    

Then for plotting with matplotlib you actually have to create a list of values
(just as when you collect real data and then type it into the computer and
then plot the single data points). For that I like to use linspace from the
numpy import:

    
    
    time_steps = np.linspace(0, t, steps) 
    # steps gives the numbers of intervals your time from 0 to t is splitted into
    

Now you create your functions x and f as functions of t. They will also have
to be of type list. And in the end you can plot what you want via:

    
    
    plt.figure(1)
    plt.plot(time_steps, f)
    plt.xlabel("x")
    plt.ylabel("y")
    plt.show()
    

But maybe you should also watch how to plot stuff in the matplotlib doc. Also
numpy has a great doc.

