为什么我无法使用 matplotlib 绘制复杂函数(通过解析获得)?
Why I am not able to plot a complex function (obtained with parsing) with matplotlib?
我正在尝试绘制一个复数函数,但我遇到了一些问题(我通常不使用 matplotlib,所以可能是一个非常愚蠢的错误)。首先,我创建了一个函数来解析给定数学函数的实部和复数部分(n 是函数索引,x 是变量):
import parser
def e_parser( real_part, imaginary_part, n, x ):
real_p = parser.expr( real_part ).compile()
imag_p = parser.expr( imaginary_part ).compile()
return complex( eval( real_p ), eval( imag_p ) )
效果很好。现在我创建了另一个函数来绘制已解析的复杂数学函数:
import numpy as np
import matplotlib.pyplot as plt
def plotter( real_part, imaginary_part, a, b, n ):
x = np.arange( a, b, ( ( a-b ) / 10 ) )
def func( x ):
return e_parser( real_part, imaginary_part, n, x )
my_label = "Wave-function for n = " + str( n )
plt.xlabel( "re" )
plt.ylabel( "im" )
plt.plot( np.real( func( x ) ), np.imag( func( x ) ), label = my_label )
plt.legend()
plt.show()
我在演示程序中尝试过:
import numpy as np
plotter( "np.sin(n*np.pi*x)", "x", 0, 1, 3 )
但是输出是:
Traceback (most recent call last):
File "main.py", line 130, in <module>
main()
File "main.py", line 21, in main
ft.plotter( "np.sin(n*np.pi*x)", "x", 0, 1, 3 )
File "/home/gianluca/WaveNCC/src/functions.py", line 208, in plotter
plt.plot( np.real( func( x ) ), np.imag( func( x ) ), label = my_label )
File "/home/gianluca/WaveNCC/src/functions.py", line 202, in func
return ut.e_parser( real_part, imaginary_part, n, x )
File "/home/gianluca/WaveNCC/src/utils.py", line 208, in e_parser
return complex( eval( real_p ), eval( imag_p ) )
File "<syntax-tree>", line 1, in <module>
TypeError: only size-1 arrays can be converted to Python scalars
如果我尝试绘制相同的函数,但虚部设置为 0:
Traceback (most recent call last):
File "main.py", line 130, in <module>
main()
File "main.py", line 21, in main
ft.plotter( "np.sin(n*np.pi*x)", 0, 0, 1, 3 )
File "/home/gianluca/WaveNCC/src/functions.py", line 211, in plotter
plt = cp.plot(
File "/home/gianluca/.local/lib/python3.8/site-packages/cplot/_main.py", line 265, in plot
extent = (x_range[0], x_range[1], y_range[0], y_range[1])
TypeError: 'int' object is not subscriptable
你知道这是怎么回事吗?谢谢。
complex
只能以标量为参数,不能以数组为参数。所以你应该使用
return eval( real_p ) + eval( imag_p ) * 1j
而不是
return complex( eval( real_p ), eval( imag_p ) )
(另外,您当然是想写 x = np.arange( a, b, ( ( b-a ) / 10 ) )
而不是 x = np.arange( a, b, ( ( a-b ) / 10 ) )
)
我正在尝试绘制一个复数函数,但我遇到了一些问题(我通常不使用 matplotlib,所以可能是一个非常愚蠢的错误)。首先,我创建了一个函数来解析给定数学函数的实部和复数部分(n 是函数索引,x 是变量):
import parser
def e_parser( real_part, imaginary_part, n, x ):
real_p = parser.expr( real_part ).compile()
imag_p = parser.expr( imaginary_part ).compile()
return complex( eval( real_p ), eval( imag_p ) )
效果很好。现在我创建了另一个函数来绘制已解析的复杂数学函数:
import numpy as np
import matplotlib.pyplot as plt
def plotter( real_part, imaginary_part, a, b, n ):
x = np.arange( a, b, ( ( a-b ) / 10 ) )
def func( x ):
return e_parser( real_part, imaginary_part, n, x )
my_label = "Wave-function for n = " + str( n )
plt.xlabel( "re" )
plt.ylabel( "im" )
plt.plot( np.real( func( x ) ), np.imag( func( x ) ), label = my_label )
plt.legend()
plt.show()
我在演示程序中尝试过:
import numpy as np
plotter( "np.sin(n*np.pi*x)", "x", 0, 1, 3 )
但是输出是:
Traceback (most recent call last):
File "main.py", line 130, in <module>
main()
File "main.py", line 21, in main
ft.plotter( "np.sin(n*np.pi*x)", "x", 0, 1, 3 )
File "/home/gianluca/WaveNCC/src/functions.py", line 208, in plotter
plt.plot( np.real( func( x ) ), np.imag( func( x ) ), label = my_label )
File "/home/gianluca/WaveNCC/src/functions.py", line 202, in func
return ut.e_parser( real_part, imaginary_part, n, x )
File "/home/gianluca/WaveNCC/src/utils.py", line 208, in e_parser
return complex( eval( real_p ), eval( imag_p ) )
File "<syntax-tree>", line 1, in <module>
TypeError: only size-1 arrays can be converted to Python scalars
如果我尝试绘制相同的函数,但虚部设置为 0:
Traceback (most recent call last):
File "main.py", line 130, in <module>
main()
File "main.py", line 21, in main
ft.plotter( "np.sin(n*np.pi*x)", 0, 0, 1, 3 )
File "/home/gianluca/WaveNCC/src/functions.py", line 211, in plotter
plt = cp.plot(
File "/home/gianluca/.local/lib/python3.8/site-packages/cplot/_main.py", line 265, in plot
extent = (x_range[0], x_range[1], y_range[0], y_range[1])
TypeError: 'int' object is not subscriptable
你知道这是怎么回事吗?谢谢。
complex
只能以标量为参数,不能以数组为参数。所以你应该使用
return eval( real_p ) + eval( imag_p ) * 1j
而不是
return complex( eval( real_p ), eval( imag_p ) )
(另外,您当然是想写 x = np.arange( a, b, ( ( b-a ) / 10 ) )
而不是 x = np.arange( a, b, ( ( a-b ) / 10 ) )
)