使用numpy和matplotlib绘制sympy函数

时间:2018-12-12 17:22:35

标签: numpy matplotlib jupyter-notebook sympy

这就是我想要做的:

# the standard broiler plate for jupyter
%matplotlib inline
from matplotlib.pyplot import *
from sympy import *
import numpy as np
init_printing()

t = symbols('t')

x1 = 2*cos(3*pi*t + pi/4)
x2 = x1.diff(t)
display(x1)
display(x2)

pi2 = 2*np.pi
vt  = np.arange(0, pi2, 0.1)
vx1 = np.zeros(len(tv))
vx2 = np.zeros(len(tv))

for n in range(0,len(vt)):
    vx1[n] = N(x1.subs(t,vt[n]))
    vx2[n] = N(x2.subs(t,vt[n]))

title('Signal')
plot(vt, vx1)
show()

title('Derivative of signal')
plot(vt, vx2)
show()

以上似乎应该可行...但是,我收到了意外的错误消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-eaccf9c2bc6b> in <module>()
     14 
     15 title('Signal')
---> 16 plot(vt, vx1)
     17 show()
     18 title('Instantanious Frequency')

C:\Python35\lib\site-packages\sympy\plotting\plot.py in plot(*args, **kwargs)
   1289     series = []
   1290     plot_expr = check_arguments(args, 1, 1)
-> 1291     series = [LineOver1DRangeSeries(*arg, **kwargs) for arg in plot_expr]
   1292 
   1293     plots = Plot(*series, **kwargs)

TypeError: 'NoneType' object is not iterable

1 个答案:

答案 0 :(得分:1)

matplotlib.pyplot具有plot方法,sympy也具有import *方法,当您进行%matplotlib inline import matplotlib.pyplot as plt import sympy import numpy as np sympy.init_printing() t = sympy.symbols('t') x1 = 2 * sympy.cos(3 * sympy.pi * t + sympy.pi / 4) x2 = x1.diff(t) display(x1) display(x2) pi2 = 2 * np.pi vt = np.arange(0, pi2, 0.1) vx1 = np.zeros(len(vt)) vx2 = np.zeros(len(vt)) for n in range(0,len(vt)): vx1[n] = sympy.N(x1.subs(t, vt[n])) vx2[n] = sympy.N(x2.subs(t,vt[n])) plt.title('Signal') plt.plot(vt, vx1) plt.show() plt.title('Derivative of signal') plt.plot(vt, vx2) plt.show() 时,它会变得有些混乱

{{1}}

enter image description here