无法使用 matplotlib 绘制平滑的复杂数学函数,为什么?
Not able to plot a smooth complex mathematical function with matplotlib, why?
我正在尝试使用 matplotlib 绘制一个复杂的函数。如果虚部为 0(这有效),此函数绘制一个普通的一维函数,如果虚部不为 0,则将整个复杂函数绘制为二维图。这是函数:
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as sci
def plotter_complex( real_part, imaginary_part, a, b, n, coefficient ):
x = np.arange( a, b, ( ( b-a ) / 10 ) )
def func( x ): #This are two functions I defined to plot the function.
return coefficient * ut.e_parser( real_part, imaginary_part, n, x )
my_label = "Normalized wave-function f(x) for n = " + str( n )
plt.figure( figsize = ( 8, 6 ), dpi = 80 )
plt.xlabel( "Re: f(x)" )
plt.ylabel( "Im: f(x)" )
plt.title( my_label )
if imaginary_part == "0":
X_Y_Spline = sci.make_interp_spline( x, func( x ) )
X = np.linspace( x.min(), x.max(), 500 )
Y = X_Y_Spline( X )
plt.xlabel( "x" )
plt.ylabel( "Re: f(x)" )
plt.plot( X, Y, color = "green" )
else:
X_Y_Spline = sci.make_interp_spline( np.real( func( x ) ), np.imag( func( x ) ) )
X = np.linspace( x.min(), x.max(), 500 )
Y = X_Y_Spline( X )
plt.plot( X, Y, color = "green" )
plt.show()
显然如果我尝试:
import numpy as np
plotter_complex( "np.cos(x)", "np.sin(x)", -1, 1, 3, 4 )
它给了我:
Traceback (most recent call last):
File "src/main.py", line 167, in <module>
main()
File "src/main.py", line 20, in main
ft.plotter_complex( "np.cos(x)", "np.sin(x)", -1, 1, 3, 4 )
File "/home/gianluca/WaveNCC/src/functions.py", line 231, in plotter_complex
X_Y_Spline = sci.make_interp_spline( np.real( func( x ) ), np.imag( func( x ) ) )
File "/home/gianluca/.local/lib/python3.8/site-packages/scipy/interpolate/_bsplines.py", line 786, in make_interp_spline
raise ValueError("Expect x to be a 1-D sorted array_like.")
ValueError: Expect x to be a 1-D sorted array_like.
没错,因为我正在尝试对两个函数进行插值。但我不明白我如何才能平滑复杂的功能。谢谢
PS:如果我尝试绘制正常的复杂函数,它会起作用。事实是我无法平滑它。
我不确定从理论的角度来看这是否正确,但是如果您添加沿 X 轴对结果进行排序,则绘制图形:
abs, ord = np.real( func( x ) ), np.imag( func( x ) )
abs, ord = zip(*sorted(zip(abs, ord)))
X_Y_Spline = sci.make_interp_spline( abs, ord )
一个有10个点的圆:
In [131]: x = np.linspace(0,2*np.pi,10)
In [132]: c = np.cos(x); s = np.sin(x)
In [133]: plt.plot(c,s)
Out[133]: [<matplotlib.lines.Line2D at 0x7f2514b98040>]
将c
和s
插值到100点:
In [136]: from scipy.interpolate import make_interp_spline
In [137]: X = np.linspace(0, 2*np.pi,100)
In [138]: C = make_interp_spline(x,c)(X)
In [139]: S = make_interp_spline(x,s)(X)
In [140]: plt.plot(C,S)
Out[140]: [<matplotlib.lines.Line2D at 0x7f2508447c70>]
直接计算那100点的对比图:
In [141]: plt.plot(np.cos(X),np.sin(X))
Out[141]: [<matplotlib.lines.Line2D at 0x7f2508455190>]
我正在尝试使用 matplotlib 绘制一个复杂的函数。如果虚部为 0(这有效),此函数绘制一个普通的一维函数,如果虚部不为 0,则将整个复杂函数绘制为二维图。这是函数:
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as sci
def plotter_complex( real_part, imaginary_part, a, b, n, coefficient ):
x = np.arange( a, b, ( ( b-a ) / 10 ) )
def func( x ): #This are two functions I defined to plot the function.
return coefficient * ut.e_parser( real_part, imaginary_part, n, x )
my_label = "Normalized wave-function f(x) for n = " + str( n )
plt.figure( figsize = ( 8, 6 ), dpi = 80 )
plt.xlabel( "Re: f(x)" )
plt.ylabel( "Im: f(x)" )
plt.title( my_label )
if imaginary_part == "0":
X_Y_Spline = sci.make_interp_spline( x, func( x ) )
X = np.linspace( x.min(), x.max(), 500 )
Y = X_Y_Spline( X )
plt.xlabel( "x" )
plt.ylabel( "Re: f(x)" )
plt.plot( X, Y, color = "green" )
else:
X_Y_Spline = sci.make_interp_spline( np.real( func( x ) ), np.imag( func( x ) ) )
X = np.linspace( x.min(), x.max(), 500 )
Y = X_Y_Spline( X )
plt.plot( X, Y, color = "green" )
plt.show()
显然如果我尝试:
import numpy as np
plotter_complex( "np.cos(x)", "np.sin(x)", -1, 1, 3, 4 )
它给了我:
Traceback (most recent call last):
File "src/main.py", line 167, in <module>
main()
File "src/main.py", line 20, in main
ft.plotter_complex( "np.cos(x)", "np.sin(x)", -1, 1, 3, 4 )
File "/home/gianluca/WaveNCC/src/functions.py", line 231, in plotter_complex
X_Y_Spline = sci.make_interp_spline( np.real( func( x ) ), np.imag( func( x ) ) )
File "/home/gianluca/.local/lib/python3.8/site-packages/scipy/interpolate/_bsplines.py", line 786, in make_interp_spline
raise ValueError("Expect x to be a 1-D sorted array_like.")
ValueError: Expect x to be a 1-D sorted array_like.
没错,因为我正在尝试对两个函数进行插值。但我不明白我如何才能平滑复杂的功能。谢谢
PS:如果我尝试绘制正常的复杂函数,它会起作用。事实是我无法平滑它。
我不确定从理论的角度来看这是否正确,但是如果您添加沿 X 轴对结果进行排序,则绘制图形:
abs, ord = np.real( func( x ) ), np.imag( func( x ) )
abs, ord = zip(*sorted(zip(abs, ord)))
X_Y_Spline = sci.make_interp_spline( abs, ord )
一个有10个点的圆:
In [131]: x = np.linspace(0,2*np.pi,10)
In [132]: c = np.cos(x); s = np.sin(x)
In [133]: plt.plot(c,s)
Out[133]: [<matplotlib.lines.Line2D at 0x7f2514b98040>]
将c
和s
插值到100点:
In [136]: from scipy.interpolate import make_interp_spline
In [137]: X = np.linspace(0, 2*np.pi,100)
In [138]: C = make_interp_spline(x,c)(X)
In [139]: S = make_interp_spline(x,s)(X)
In [140]: plt.plot(C,S)
Out[140]: [<matplotlib.lines.Line2D at 0x7f2508447c70>]
直接计算那100点的对比图:
In [141]: plt.plot(np.cos(X),np.sin(X))
Out[141]: [<matplotlib.lines.Line2D at 0x7f2508455190>]