ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 27 Feb 2013 09:50:07 -06003d Complex function plothttp://ask.sagemath.org/question/9843/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/](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 ?Sat, 23 Feb 2013 23:29:57 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/Comment by kcrisman for <p>I want to plot a complex function in 3d. And colour it according to the real or imaginary part as in these pictures.
<a href="http://functions.wolfram.com/ElementaryFunctions/Sin/visualizations/5/">http://functions.wolfram.com/ElementaryFunctions/Sin/visualizations/5/</a></p>
<p>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.</p>
<p>Is there a way to do it with sage ?</p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18170#post-id-18170You may also find http://trac.sagemath.org/sage_trac/ticket/12212 relevant.Sun, 24 Feb 2013 13:29:29 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18170#post-id-18170Answer by koukourikos for <p>I want to plot a complex function in 3d. And colour it according to the real or imaginary part as in these pictures.
<a href="http://functions.wolfram.com/ElementaryFunctions/Sin/visualizations/5/">http://functions.wolfram.com/ElementaryFunctions/Sin/visualizations/5/</a></p>
<p>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.</p>
<p>Is there a way to do it with sage ?</p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?answer=14594#post-id-14594I found that using matplotlib is quite easy to colour a surface according to whatever function you want. <strike> I didn't try to run it inside SAGE but I dont think that it would be very difficult. </strike>
#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:
![http://oi47.tinypic.com/34ijyfs.jpg](http://oi47.tinypic.com/34ijyfs.jpg)
Sun, 24 Feb 2013 20:35:13 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?answer=14594#post-id-14594Comment by koukourikos for <p>I found that using matplotlib is quite easy to colour a surface according to whatever function you want. <strike> I didn't try to run it inside SAGE but I dont think that it would be very difficult. </strike></p>
<pre><code>#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')
</code></pre>
<p>and the result:
<img alt="http://oi47.tinypic.com/34ijyfs.jpg" src="http://oi47.tinypic.com/34ijyfs.jpg"/></p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18140#post-id-18140@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#2402Wed, 27 Feb 2013 06:17:17 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18140#post-id-18140Comment by kcrisman for <p>I found that using matplotlib is quite easy to colour a surface according to whatever function you want. <strike> I didn't try to run it inside SAGE but I dont think that it would be very difficult. </strike></p>
<pre><code>#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')
</code></pre>
<p>and the result:
<img alt="http://oi47.tinypic.com/34ijyfs.jpg" src="http://oi47.tinypic.com/34ijyfs.jpg"/></p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18138#post-id-18138What 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.Wed, 27 Feb 2013 09:50:07 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18138#post-id-18138Comment by kcrisman for <p>I found that using matplotlib is quite easy to colour a surface according to whatever function you want. <strike> I didn't try to run it inside SAGE but I dont think that it would be very difficult. </strike></p>
<pre><code>#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')
</code></pre>
<p>and the result:
<img alt="http://oi47.tinypic.com/34ijyfs.jpg" src="http://oi47.tinypic.com/34ijyfs.jpg"/></p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18164#post-id-18164True, 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!Mon, 25 Feb 2013 01:48:53 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18164#post-id-18164Answer by achrzesz for <p>I want to plot a complex function in 3d. And colour it according to the real or imaginary part as in these pictures.
<a href="http://functions.wolfram.com/ElementaryFunctions/Sin/visualizations/5/">http://functions.wolfram.com/ElementaryFunctions/Sin/visualizations/5/</a></p>
<p>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.</p>
<p>Is there a way to do it with sage ?</p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?answer=14591#post-id-14591My 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/how-to-color-a-3d-plot-by-z-level
Sun, 24 Feb 2013 04:14:57 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?answer=14591#post-id-14591Comment by achrzesz for <p>My colouring is based on the z=f(x,y) value of the plotted function. Compare:</p>
<pre><code>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)
</code></pre>
<p>or:</p>
<pre><code>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)
</code></pre>
<p><a href="http://ask.sagemath.org/question/1405/how-to-color-a-3d-plot-by-z-level">http://ask.sagemath.org/question/1405...</a></p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18171#post-id-18171http://ask.sagemath.org/question/1767/coloring-surfaces-in-plot3dSun, 24 Feb 2013 07:59:25 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18171#post-id-18171Comment by koukourikos for <p>My colouring is based on the z=f(x,y) value of the plotted function. Compare:</p>
<pre><code>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)
</code></pre>
<p>or:</p>
<pre><code>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)
</code></pre>
<p><a href="http://ask.sagemath.org/question/1405/how-to-color-a-3d-plot-by-z-level">http://ask.sagemath.org/question/1405...</a></p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18166#post-id-18166Thanks! Matplotlib link was a good place to start my search.Sun, 24 Feb 2013 20:37:41 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18166#post-id-18166Comment by koukourikos for <p>My colouring is based on the z=f(x,y) value of the plotted function. Compare:</p>
<pre><code>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)
</code></pre>
<p>or:</p>
<pre><code>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)
</code></pre>
<p><a href="http://ask.sagemath.org/question/1405/how-to-color-a-3d-plot-by-z-level">http://ask.sagemath.org/question/1405...</a></p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18172#post-id-18172yes 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. Sun, 24 Feb 2013 07:18:10 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18172#post-id-18172Answer by achrzesz for <p>I want to plot a complex function in 3d. And colour it according to the real or imaginary part as in these pictures.
<a href="http://functions.wolfram.com/ElementaryFunctions/Sin/visualizations/5/">http://functions.wolfram.com/ElementaryFunctions/Sin/visualizations/5/</a></p>
<p>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.</p>
<p>Is there a way to do it with sage ?</p>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?answer=14590#post-id-14590For 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)Sat, 23 Feb 2013 23:59:51 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?answer=14590#post-id-14590Comment by koukourikos for <p>For example:</p>
<pre><code>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)
</code></pre>
http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18174#post-id-18174Thank 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 Sun, 24 Feb 2013 03:35:43 -0600http://ask.sagemath.org/question/9843/3d-complex-function-plot/?comment=18174#post-id-18174