使用Sympy方程进行绘图 [英] Using Sympy Equations for Plotting
本文介绍了使用Sympy方程进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
创建Sympy方程,进行导数运算然后绘制该方程结果的最佳方法是什么?
What is the best way to create a Sympy equation, do something like take the derivative, and then plot the results of that equation?
我有我的符号方程式,但无法弄清楚如何绘制一组用于绘图的值.这是我的代码:
I have my symbolic equation, but can't figure out how to make an array of values for plotting. Here's my code:
from sympy import symbols
import matplotlib.pyplot as mpl
t = symbols('t')
x = 0.05*t + 0.2/((t - 5)**2 + 2)
nums = []
for i in range(1000):
nums.append(t)
t += 0.02
plotted = [x for t in nums]
mpl.plot(plotted)
mpl.ylabel("Speed")
mpl.show()
在我的情况下,我只是计算了该方程的导数,现在我想绘制速度x
,因此这相当简化.
In my case I just calculated the derivative of that equation, and now I want to plot the speed x
, so this is fairly simplified.
推荐答案
您可以使用 lambdify()
.
You can use numpy.linspace()
to create the values of the x axis (x_vals
in the code below) and lambdify()
.
from sympy import symbols
from numpy import linspace
from sympy import lambdify
import matplotlib.pyplot as mpl
t = symbols('t')
x = 0.05*t + 0.2/((t - 5)**2 + 2)
lam_x = lambdify(t, x, modules=['numpy'])
x_vals = linspace(0, 10, 100)
y_vals = lam_x(x_vals)
mpl.plot(x_vals, y_vals)
mpl.ylabel("Speed")
mpl.show()
( asmeurer 和
或者,您可以使用sympy的plot()
:
Alternatively, you can use sympy's plot()
:
from sympy import symbols
from sympy import plot
t = symbols('t')
x = 0.05*t + 0.2/((t - 5)**2 + 2)
plot(x, (t, 0, 10), ylabel='Speed')
这篇关于使用Sympy方程进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文