Python 在matplotlib中用输入绘制抛物线
所以我用python创建了一个二次方程求解器,我想要一个与之配套的图。我想用这张图来描绘抛物线 a=二次方程中a的输入 b=二次方程中b的输入 c=二次方程中c的输入 y=-b/2*a(向量)Python 在matplotlib中用输入绘制抛物线,python,matplotlib,math,graphing,Python,Matplotlib,Math,Graphing,所以我用python创建了一个二次方程求解器,我想要一个与之配套的图。我想用这张图来描绘抛物线 a=二次方程中a的输入 b=二次方程中b的输入 c=二次方程中c的输入 y=-b/2*a(向量) 导入数学 a=int(输入('input a:')) b=int(输入('input b:')) c=int(输入('input c:')) discrim=int((b*b)-4*a*c) posi=(-b+(math.sqrt(discrim))/(2*a) 负=(-b-(数学sqrt(discrim
导入数学
a=int(输入('input a:'))
b=int(输入('input b:'))
c=int(输入('input c:'))
discrim=int((b*b)-4*a*c)
posi=(-b+(math.sqrt(discrim))/(2*a)
负=(-b-(数学sqrt(discrim))/(2*a)
如果b>0、c>0且posi<0:
posi=-posi
如果neg<0:
负=-负
y=-b/2*a
向量=(负+正)/2,y)
有了这段代码,我就有了x截距和向量,这足以创建一条抛物线,我只是不知道如何使用MatPlotLib将这些值转换成图形。请提出任何问题 我对您的代码做了一些更正。下面的代码将绘制任意抛物线。可以在绘图中添加零点和极值点
import matplotlib.pyplot as plt
import math
import numpy as np
a = int(input('Input a: '))
b = int(input('Input b: '))
c = int(input('Input c: '))
# calculate delta and zero points
delta = b**2 - 4*a*c
if delta > 0:
x_1 = (-b + math.sqrt(delta))/(2*a)
x_2 = (-b - math.sqrt(delta))/(2*a)
if delta == 0:
x_0 = -b/(2*a)
else:
pass
# calculate parabola extreme coordinates
p = -b/(2*a)
q = -delta/(4*a)
extreme = [p,q]
# define parabola function
def parabola(x,a,b,c):
y = a*x**2 + b*x + c
return y
# plot function
x = np.linspace(int(p)-5,int(p)+5,100)
y = parabola(x,a,b,c)
plt.plot(x,y)
plt.axhline(y=0, color='black', linestyle='-')
plt.axvline(x=0, color='black', linestyle='-')
plt.text(p-0.5, q-3, '[' + str(round(p,2)) +',' + str(round(q,2)) + ']',color='orange', fontsize=9)
plt.plot(p, q, marker="o")
if delta > 0:
plt.plot(x_1, 0, marker="o", color='green')
plt.text(x_1 - 0.5, 2, '[' + str(round(x_1,2)) + ']', color='green', fontsize=9)
plt.plot(x_2, 0, marker="o", color='green')
plt.text(x_2 - 0.5, 2, '[' + str(round(x_2,2)) + ']', color='green', fontsize=9)
if delta == 0:
plt.plot(x_0, 0, marker="o", color='green')
plt.text(x_0 - 0.5, 2, '[' + str(round(x_0,2)) + ']', color='green', fontsize=9)
plt.show()
样本结果:
您需要根据输入参数绘制简单的抛物线?基本上是的,但我在这方面有困难。谢谢!这正是我想要的。另外一个测试是,在x_1,x_2存在的情况下,它们是否在绘制的范围内可见。类似于
如果delta>0:x=np.linspace(x_2-5,x_1+5100)
import matplotlib.pyplot as plt
import math
import numpy as np
a = int(input('Input a: '))
b = int(input('Input b: '))
c = int(input('Input c: '))
# calculate delta and zero points
delta = b**2 - 4*a*c
if delta > 0:
x_1 = (-b + math.sqrt(delta))/(2*a)
x_2 = (-b - math.sqrt(delta))/(2*a)
if delta == 0:
x_0 = -b/(2*a)
else:
pass
# calculate parabola extreme coordinates
p = -b/(2*a)
q = -delta/(4*a)
extreme = [p,q]
# define parabola function
def parabola(x,a,b,c):
y = a*x**2 + b*x + c
return y
# plot function
x = np.linspace(int(p)-5,int(p)+5,100)
y = parabola(x,a,b,c)
plt.plot(x,y)
plt.axhline(y=0, color='black', linestyle='-')
plt.axvline(x=0, color='black', linestyle='-')
plt.text(p-0.5, q-3, '[' + str(round(p,2)) +',' + str(round(q,2)) + ']',color='orange', fontsize=9)
plt.plot(p, q, marker="o")
if delta > 0:
plt.plot(x_1, 0, marker="o", color='green')
plt.text(x_1 - 0.5, 2, '[' + str(round(x_1,2)) + ']', color='green', fontsize=9)
plt.plot(x_2, 0, marker="o", color='green')
plt.text(x_2 - 0.5, 2, '[' + str(round(x_2,2)) + ']', color='green', fontsize=9)
if delta == 0:
plt.plot(x_0, 0, marker="o", color='green')
plt.text(x_0 - 0.5, 2, '[' + str(round(x_0,2)) + ']', color='green', fontsize=9)
plt.show()