3d Complex function plot

I want to plot a complex function in 3d. And colour it according to the real or imaginary part as in these pictures. http://functions.wolfram.com/ElementaryFunctions/Sin/visualizations/5/

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 close merge delete

1
( 2013-02-24 13:29:29 -0500 )edit

Sort by » oldest newest most voted

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 http://stackoverflow.com/a/6543777
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)
Z=R.real
T=R.imag
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,
facecolors=cm.jet(N),
# Colorbar see http://stackoverflow.com/a/6601210
m = cm.ScalarMappable(cmap=cm.jet, norm=surf.norm)
m.set_array(T)
p=plt.colorbar(m)
p.set_label(' $\mathrm{Im(f(z))}$')

fig.set_size_inches(14,7) #http://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib
#plt.show() # if you run it as a python script
plt.savefig('test.png')


and the result:

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!

( 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 : http://ask.sagemath.org/question/1589/matplotlib-cannot-create-a-graph?answer=2402#2402

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

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

For example:

sage: var('x y');
sage: cmsel = [colormaps['gnuplot2'](i) for i in sxrange(0,1,0.02)]

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

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

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


or:

sage: cmsel = [colormaps['gnuplot2'](i) for i in sxrange(0.05,0.75,0.02)]


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.

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

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

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

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