从滑块在Matplotlib(Python)中打印复杂函数?

2024-09-25 12:27:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python非常陌生,仍然在学习matplotlib的基础知识。我理解如何“正常地”绘制一些东西,但是对于一个任务,我需要绘制一个复杂的函数,并且能够通过变量控制进入该函数的变量

例如:如果我有变量a、b和c

我想画一个复杂的函数:f(xj)=(a)(b)(xj)^c其中j=sqrt(-1)

(或者任何你想要的功能,真的,我只是从头顶上编造出来的)

目标是将它们绘制为单独的线(即,实部为一条线,虚部为另一条线),但能够通过滑块控制a、b和c。我该怎么做?变量的范围可以是任何东西,因为这只是一个一般的操作问题

我知道.real和.imag命令,但我不知道如何对在滑块上控制变量的函数执行这些命令

非常感谢您的帮助,谢谢


Tags: 函数命令功能目标matplotlib绘制sqrtreal
2条回答

如果我们使用了你给出的f(xj) = (a)(b)(xj)^c; j = sqrt(-1)示例,并假设x轴将是域值(xj),那么可以使用此代码绘制实数值和复数值

#matplotlib.pyplot for the graph
import matplotlib.pyplot as plt
#numpy for the arrays and math-on-array stuff
import numpy as np
#defining the domain
x = np.arange(-5,5,0.1)
#f, the function
def f(x,a=1,b=1,c=1):
    return a*b*(x*1j)**c
#plot the actual graph with the label
plt.plot(x,np.imag(f(x*1j)), label='f(xj) real part')
plt.plot(x,np.real(f(x*1j)), label='f(xj) imaginary part')
#show the label
plt.legend()
#show the graph
plt.show()

然后,对于滑块,我们可以从matplotlib.widget添加滑块小部件(在documentation中找到关于它的更多信息),因此我们可以使用此代码

#import matplotlib.pyplot for the graph
import matplotlib.pyplot as plt
#import numpy for the arrays and math-on-array stuff
import numpy as np
#import Slider from matplotlib.widgets
from matplotlib.widgets import Slider
#defining the domain
x = np.arange(-5,5,0.1)
#f, the function
def f(x,a=1,b=1,c=1):
    return a*b*(x*1j)**c
#adjusting the main plot dimensions
plt.subplots_adjust(bottom=0.18, top=0.95)
#plot the actual graph with the label (and assigong them into variables)
real_part, = plt.plot(x,np.real(f(x*1j)), label='f(xj) real part')
imag_part, = plt.plot(x,np.imag(f(x*1j)), label='f(xj) imaginary part')
#show the label
plt.legend()
#making sliders
axSlider1 = plt.axes([0.1, 0.02, 0.8, 0.03])
Slider1 = Slider(axSlider1,"a",valmin=0,valmax=2,valinit=1, valstep=0.01,)

axSlider2 = plt.axes([0.1, 0.06, 0.8, 0.03])
Slider2 = Slider(axSlider2,"b",valmin=0,valmax=2,valinit=1, valstep=0.01,)

axSlider3 = plt.axes([0.1, 0.10, 0.8, 0.03])
Slider3 = Slider(axSlider3,"c",valmin=0,valmax=2,valinit=1, valstep=0.01,)
#the functions of how the value of the slider affect the graph
def A(aval):
    aval = Slider1.val
    real_part.set_ydata(np.real(f(x*1j,aval,Slider2.val,Slider3.val)))
    imag_part.set_ydata(np.imag(f(x*1j,aval,Slider2.val,Slider3.val)))
    plt.draw()
def B(bval):
    bval = Slider2.val
    real_part.set_ydata(np.real(f(x*1j,Slider1.val,bval,Slider3.val)))
    imag_part.set_ydata(np.imag(f(x*1j,Slider1.val,bval,Slider3.val)))    
    plt.draw()
def C(cval):
    cval = Slider3.val
    real_part.set_ydata(np.real(f(x*1j,Slider1.val,Slider2.val,cval)))
    imag_part.set_ydata(np.imag(f(x*1j,Slider1.val,Slider2.val,cval)))    
    plt.draw()
#applying those fucntions into sliders
Slider1.on_changed(A)
Slider2.on_changed(B)
Slider3.on_changed(C)
#show the graph
plt.show()

但是,使用其他图形更有效,例如以xj为域的3D图形,以y轴和z轴上的实数和虚数为范围的复数

复值函数通常也将复数作为参数,例如,函数

f(z) = a * b * (z * j)^c

由于这是将二维实体映射到二维实体,因此视觉表示将不是普通的“图形”。表示复函数的两种方法是布置复平面,到处计算函数值和

我的Python包cplot包括这两个方面。例如,这将绘制正弦函数:

import cplot
import numpy as np

cplot.show(np.sin, (-5.0, +5.0), (-5.0, +5.0), 1000)

enter image description here

相关问题 更多 >