2016-02-15 196 views
-2

如何使用此功能構建圖形?謝謝! :)我試過,但我越來越糊塗了我應該怎麼使用:https://stackoverflow.com/questions/30553585/graphing-a-parabola-using-matplotlib-in-python#=Python使用Matplotlib繪製拋物線圖

def quadratic (a, b, c): 
try: 
    d = b**2-4*a*c 
    convex_point = -b/(2*a) 
    if(d == 0): 
     convex_point = -b/(2*a) #it is the x-interceptor when det is 0 
     print('the convex point is at',convex_point, 'on the x-axis | the parabola intersect the y-axis at',c, '| the determinant is 0'); 
    elif(d < 0): 
     print('Determinant is', d,'and if determinant is smaller than zero, there is no real solution'); 
    else: 
     x_positive = (-b+ math.sqrt(d))/(2*a); # y = 0 
     x_negative = (-b- math.sqrt(d))/(2*a); # y = 0 
     print('the convex points is at',convex_point, 'on the x-axis |x_positive',x_positive,' |x_negative',x_negative,'| the parabola intersect the y-axis at',c) 


except: 
    print('try: import math'); 

回答

0
def quadratic (a, b, c): 
try: 
    import matplotlib.pyplot as plt 
    import math 
    import numpy as np 
    d = b**2-4*a*c 
    convex_point = -b/(2*a) 
    x = np.linspace(-50, 50, 1000); 
    y = a**2 + b*x + c 
    fig, ax = plt.subplots(); 
    ax.plot(x, y) 
    if(d == 0): 
     convex_point = -b/(2*a) #it is the x-interceptor when det is 0 
     print('the convex point is at',convex_point, 'on the x-axis | the parabola intersect the y-axis at',c, '| the determinant is 0'); 
    elif(d < 0): 
     print('Determinant is', d,'and if determinant is smaller than zero, there is no real solution'); 
    else: 
     x_positive = (-b+ math.sqrt(d))/(2*a); # y = 0 
     x_negative = (-b- math.sqrt(d))/(2*a); # y = 0 
     print('the convex points is at',convex_point, 'on the x-axis |x_positive',x_positive,' |x_negative',x_negative,'| the parabola intersect the y-axis at',c); 



except: 
    print('try: import math') 

只是它不顯示圖形:S

+1

嘗試在你的'ax.plot'行後面添加一個'plt.show()' – tom

+0

我試過了,但是它給我帶來了一個異常,當我刪除它時,它又恢復了。 – Francesco

+0

它拋出了什麼錯誤? – tom

0
def quadratic (a, b, c): 
try: 
    import matplotlib.pyplot as plt 
    import math 
    import numpy as np 
    d = b**2-4*a*c 
    convex_point = -b/(2*a) 
    x = np.linspace(-10, 10, 1000); 
    y = a**2 + b*x + c 
    fig, ax = plt.subplots(); 
    ax.plot(x, y); 
    plt.show() 
    if(d == 0): 
     convex_point = -b/(2*a) #it is the x-interceptor when det is 0 
     print('the convex point is at',convex_point, 'on the x-axis | the parabola intersect the y-axis at',c, '| the determinant is 0'); 
    elif(d < 0): 
     print('Determinant is', d,'and if determinant is smaller than zero, there is no real solution'); 
    else: 
     x_positive = (-b+ math.sqrt(d))/(2*a); # y = 0 
     x_negative = (-b- math.sqrt(d))/(2*a); # y = 0 
     print('the convex points is at',convex_point, 'on the x-axis |x_positive',x_positive,' |x_negative',x_negative,'| the parabola intersect the y-axis at',c); 
except: 
    print('try: import math') 

此代碼工作:)