简体   繁体   English

从 slider 中绘制 Matplotlib (Python) 中的复杂函数?

[英]Plotting Complex Functions in Matplotlib (Python) from a slider?

I'm pretty new to Python, and still learning the basics around matplotlib.我对 Python 还是很陌生,还在学习 matplotlib 的基础知识。 I understand how one would plot something "normally", but for a task I'm going to need to plot a complex function, and be able to control the variables going into the that function via variables. I understand how one would plot something "normally", but for a task I'm going to need to plot a complex function, and be able to control the variables going into the that function via variables.

For example: if I had the variables a, b, and c,例如:如果我有变量 a、b 和 c,

and I wanted to plot the complex function: f(xj) = (a)(b)(xj)^c where j = sqrt(-1)我想 plot 复杂的 function: f(xj) = (a)(b)(xj)^c where j = sqrt(-1)

(or any function you want, really, I just made this up off the top of my head). (或者你想要的任何 function,真的,我只是在脑海中编造了这个)。

The goal is to plot them as separate lines (aka, the real component as one line, the imaginary component as the other), but to be able to control a, b and c via sliders.目标是将 plot 它们作为单独的线(又名,实部作为一条线,虚部作为另一条线),但能够通过滑块控制 a、b 和 c。 How would I do that?我该怎么做? Ranges for the variables can be anything, since this is just a general how-to question.变量的范围可以是任何东西,因为这只是一个一般的操作方法问题。

I know about the.real and the.imag commands, but I don't know how to carry that out for a function with variables controlled on a slider.我知道 the.real 和 the.imag 命令,但我不知道如何为 function 执行该命令,变量控制在 slider 上。

Any help would be very much appreciated, thanks.任何帮助将不胜感激,谢谢。

If we used the example you put of f(xj) = (a)(b)(xj)^c; j = sqrt(-1)如果我们使用你提出的例子f(xj) = (a)(b)(xj)^c; j = sqrt(-1) f(xj) = (a)(b)(xj)^c; j = sqrt(-1) and assuming that the x-axis will be the domain values (xj) then it is possible to use this code to plot both of the real value and complex one with this code f(xj) = (a)(b)(xj)^c; j = sqrt(-1)并假设 x 轴将是域值(xj)然后可以使用此代码来 plot 使用此代码的实值和复数

#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()

then for the slider, we can add slider widget from matplotlib.widget (find more info about it in the documentation ), so we can use this code然后对于 slider,我们可以从 matplotlib.widget 中添加 slider 小部件(在文档中找到有关它的更多信息),所以我们可以使用此代码

#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()

However, it is more efficient to use other graphs, as a 3D graph with the xj as a domain and the complex numbers with real and imaginary values in the y and z axes as a range.但是,使用其他图更有效,例如 3D 图,其中xj作为域,复数在 y 和 z 轴上具有实部和虚部作为范围。

A function of complex values often also takes complex numbers as arguments, eg, your function.复数值的 function 通常也将复数作为 arguments,例如,您的 function。

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

Since this is mapping a two-dimensional entity to a two-dimensional entity, a visual representation won't be an ordinary "graph".由于这是将二维实体映射到二维实体,因此视觉表示不会是普通的“图形”。 Two ways of representing a complex function is to lay out the complex plane, compute the function values everywhere and表示复数 function 的两种方法是布置复平面,计算 function 的所有值和

  • plot contour lines at constant angle and/or constant absolute value plot 等角线和/或绝对值不变的等高线
  • domain coloring域着色

A Python package of mine, cplot , includes both.我的 Python package cplot包括两者。 For example, this plots the sine function:例如,绘制正弦 function:

import cplot
import numpy as np

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM