简体   繁体   中英

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

I'm pretty new to Python, and still learning the basics around 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.

For example: if I had the variables a, b, and c,

and I wanted to plot the complex 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).

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. 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.

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

#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

#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.

A function of complex values often also takes complex numbers as arguments, eg, your 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

  • plot contour lines at constant angle and/or constant absolute value
  • domain coloring

A Python package of mine, cplot , includes both. For example, this plots the sine function:

import cplot
import numpy as np

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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