Function is Not Graphing in the Range I want it to

Question
x = [np.sqrt(.16425), 0.54455260535599315,
 0.70689815390903377,
 0.90923319341079933,
 0.82807306440917394,
 1.1290283433111854]
x

y = [0.8187249999999999,
     1.0933652892561987,
 1.419317391304348,
 1.8247052631578946,
 1.6618111111111107,
 2.2666052631578943]

def graph(formula, x_range):  
    x =np.array(x_range)  
    y =eval(formula)
    plt.plot(x, y)  

graph('0.00030796 + 2.00697*x', range(np.divide(1,2),2))

plt.scatter(x,y)


plt.xlabel('Sqaure Root of Length (m^(1/2))')
plt.ylabel('Approximate Average Period (s)')

plt.show()

All I am trying to do is plot some data points and a theory line to accompany it, but my range only accepts integer values so I have no clue how to make a nice graph with a nice range to show my data points and the theory line through them. Can someone help me out?


Show source
| numpy   | python   | graph   2016-11-05 02:11 1 Answers

Answers to Function is Not Graphing in the Range I want it to ( 1 )

  1. 2016-11-05 09:11

    You code has more issues than what you are describing. Here is an implementation that works.

    def graph(formula, x_range):  
        x =np.linspace(*x_range,100)  
        y =lambda x: eval(formula)
        plt.plot(x, y(x))  
    
    graph('0.00030796 + 2.00697*x', (np.divide(1,2),2))
    plt.scatter(x,y)
    

    enter image description here

Leave a reply to - Function is Not Graphing in the Range I want it to

◀ Go back