Ask Your Question

3d Complex function plot

asked 2013-02-23 23:29:57 -0500

koukourikos gravatar image

I want to plot a complex function in 3d. And colour it according to the real or imaginary part as in these pictures.

I know that I can use the complex_plot command to get the 2d plots. But I want the 3d version of the plot like in the above link.

Is there a way to do it with sage ?

edit retag flag offensive close merge delete


kcrisman gravatar imagekcrisman ( 2013-02-24 13:29:29 -0500 )edit

3 answers

Sort by » oldest newest most voted

answered 2013-02-24 20:35:13 -0500

koukourikos gravatar image

updated 2013-02-27 06:41:36 -0500

I found that using matplotlib is quite easy to colour a surface according to whatever function you want. I didn't try to run it inside SAGE but I don’t think that it would be very difficult.

#Complex Sinus function  with coloring based to imaginary part  
# Based on this comment
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-2*3.14, 2*3.14, 0.1)
Y = np.arange(-2, 2, 0.1)
X, Y = np.meshgrid(X, Y)
R=np.sin(X + 1j*Y)
N = np.abs(T/T.max())  # normalize 0..1
plt.title(' $\mathrm{f(z)=sin(z)}$')
plt.xlabel(' $\mathrm{Re(z)}$')
plt.ylabel(' $\mathrm{Im(z)}$')
surf = ax.plot_surface(
    X, Y, Z, rstride=1, cstride=1,
    linewidth=0, antialiased=True, shade=False)
# Colorbar see
m = cm.ScalarMappable(cmap=cm.jet, norm=surf.norm)
p.set_label(' $\mathrm{Im(f(z))}$')

fig.set_size_inches(14,7) # # if you run it as a python script

and the result:

edit flag offensive delete link more


True, but (thus far) we don't have matplotlib as a backend to our 3d plots (since it didn't have this capability when we implemented them). If you could help us with that it would be great!

kcrisman gravatar imagekcrisman ( 2013-02-25 01:48:53 -0500 )edit

@kcrisman It appears it very easy to get it work in SAGE just adding : plt.savefig('test.png') as last line in my code. Produces the plot in SAGE. source :

koukourikos gravatar imagekoukourikos ( 2013-02-27 06:17:17 -0500 )edit

What I meant was "native" Sage, sorry. Of course mpl works very nicely in Sage! But our own `plot3d` functions etc. aren't set up to use this (yet) in the backend.

kcrisman gravatar imagekcrisman ( 2013-02-27 09:50:07 -0500 )edit

answered 2013-02-23 23:59:51 -0500

achrzesz gravatar image

For example:

sage: var('x y');
sage: cmsel = [colormaps['gnuplot2'](i) for i in sxrange(0,1,0.02)]
sage: plot3d(lambda x,y:(sin(x+I*y)).imag_part(),(x,-3*pi,3*pi),(y,0,2),adaptive=True,color=cmsel)
edit flag offensive delete link more


Thank you. But it's not exactly what I want. Your colouring is based on the y values but I want to be based on the real_part(sin(z)) values

koukourikos gravatar imagekoukourikos ( 2013-02-24 03:35:43 -0500 )edit

answered 2013-02-24 04:14:57 -0500

achrzesz gravatar image

updated 2013-02-24 04:45:44 -0500

My colouring is based on the z=f(x,y) value of the plotted function. Compare:

sage: var('x y');
sage: cmsel = [colormaps['gnuplot2'](i) for i in sxrange(0,1,0.02)]
sage: plot3d(lambda x,y:cos(sqrt(abs((x+I*y)*(x-I*y)))),(x,-2*pi,2*pi),(y,-2*pi,2*pi),adaptive=True,color=cmsel)


sage: cmsel = [colormaps['gnuplot2'](i) for i in sxrange(0.05,0.75,0.02)]
sage: plot3d(lambda x,y:sin(x+I*y).imag(),(x,-2*pi,2*pi),(y,-2,2),adaptive=True,color=cmsel,mesh=True)

edit flag offensive delete link more


yes it is based on the f(z) which means on the imaginary part of z but I want to be based on the real part of z. In other words I want a plot of f(z)=Im(sin(z)) and its colouring respect to the Re(sin(z)). Sorry for the confusing explanation.

koukourikos gravatar imagekoukourikos ( 2013-02-24 07:18:10 -0500 )edit

achrzesz gravatar imageachrzesz ( 2013-02-24 07:59:25 -0500 )edit

Thanks! Matplotlib link was a good place to start my search.

koukourikos gravatar imagekoukourikos ( 2013-02-24 20:37:41 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-02-23 23:29:57 -0500

Seen: 1,875 times

Last updated: Feb 27 '13