Ask Your Question

# 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

## Comments

1
( 2013-02-24 20:29:29 +0200 )edit

## 3 Answers

Sort by » oldest newest most voted

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)

more

## Comments

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 10:35:43 +0200 )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)]
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)


or:

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)


http://ask.sagemath.org/question/1405...

more

## Comments

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 14:18:10 +0200 )edit

http://ask.sagemath.org/question/1767/coloring-surfaces-in-plot3d

( 2013-02-24 14:59:25 +0200 )edit

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

( 2013-02-25 03:37:41 +0200 )edit

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),
linewidth=0, antialiased=True, shade=False)
# 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

## Comments

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 08:48:53 +0200 )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 13:17:17 +0200 )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 16:50:07 +0200 )edit

## Your Answer

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

Add Answer

## Stats

Asked: 2013-02-24 06:29:57 +0200

Seen: 4,210 times

Last updated: Feb 27 '13