Plotting Functions of 3 variables

707 views
Skip to first unread message

ObsessiveMathsFreak

unread,
May 5, 2011, 12:25:48 PM5/5/11
to sage-support
I currently have a function of three variables w=f(x,y,z), which I
would like to plot in 3D if possible (e.g. via coloured plots)

Does sage currently have support for plotting such functions?
plot_vector_field3d has support for plotting vector fields of three
variables, but there does not seem to be an equivilent function for
_scalar_ functions as far as I can see.

In addition, do any such functions have support for plotting in 3D
using different coordinate systems. For example, support for
redefining f in spherical coordinates w=f(r,theta,phi)?

kcrisman

unread,
May 5, 2011, 1:12:13 PM5/5/11
to sage-support


On May 5, 12:25 pm, ObsessiveMathsFreak
<obsessivemathsfr...@gmail.com> wrote:
> I currently have a function of three variables w=f(x,y,z), which I
> would like to plot in 3D if possible (e.g. via coloured plots)
>
> Does sage currently have support for plotting such functions?
> plot_vector_field3d has support for plotting vector fields of three
> variables, but there does not seem to be an equivilent function for
> _scalar_ functions as far as I can see.

Are you thinking of doing a contour plot? I think it would be hard to
see the colors and discern them if you just filled up a box with
different-colored pixels.

There are coordinate transformations and plots, I believe - see
http://www.sagemath.org/doc/reference/sage/plot/plot3d/plot3d.html for
many examples, search for 'transformation='

- kcrisman

achrzesz

unread,
May 5, 2011, 1:28:47 PM5/5/11
to sage-support
Maybe a series of implicit_plot3d with w=w0,w1,...
will be helpful?

Kelvin Li

unread,
May 5, 2011, 1:42:35 PM5/5/11
to sage-support
> > > I currently have a function of three variables w=f(x,y,z), which I
> > > would like to plot in 3D if possible (e.g. via coloured plots)

Not sure about color, but `implicit_plot3d` can produce 3D contour
plots.

http://www.sagemath.org/doc/reference/sage/plot/plot3d/implicit_plot3d.html

> > > Does sage currently have support for plotting such functions?
> > > plot_vector_field3d has support for plotting vector fields of three
> > > variables, but there does not seem to be an equivilent function for
> > > _scalar_ functions as far as I can see.

As a hack, you can plot a vector field where the scalar output of your
function simply drives the z-component of the vectors. :-)

> > There are coordinate transformations and plots, I believe - seehttp://www.sagemath.org/doc/reference/sage/plot/plot3d/plot3d.htmlfor
> > many examples, search for 'transformation='

Does `implicit_plot3d` support transforms?

> > - kcrisman

-- Kelvin

ObsessiveMathsFreak

unread,
May 5, 2011, 1:43:21 PM5/5/11
to sage-support
Thanks but, these methods are not wholly adequate for my purposes.

I have been investigating the mayavi python package and the examples
given here
http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab_case_studies.html#mlab-case-studies

Apparently sagemath has support for mayavi, but when I follow the
instructions here
http://www.sagemath.org/doc/numerical_sage/installation_osx.html

I get an error

sage: install_package("python-2.5.1-framework")
ValueError: There is no package name starting with 'python-2.5.1-
framework'.

And so on for the other commands in the page.

I'm wondering if mayavi has been removed from sage, or whether its
functionality is now part of the core packages?

kcrisman

unread,
May 5, 2011, 3:24:38 PM5/5/11
to sage-support


On May 5, 1:43 pm, ObsessiveMathsFreak <obsessivemathsfr...@gmail.com>
wrote:
> Thanks but, these methods are not wholly adequate for my purposes.
>
> I have been investigating the mayavi python package and the examples
> given herehttp://code.enthought.com/projects/mayavi/docs/development/html/mayav...
>
> Apparently sagemath has support for mayavi, but when I follow the
> instructions herehttp://www.sagemath.org/doc/numerical_sage/installation_osx.html
>
> I get an error
>
> sage: install_package("python-2.5.1-framework")
> ValueError: There is no package name starting with 'python-2.5.1-
> framework'.

Looking at

./sage -optional

I don't get these. I think they are all experimental.

> And so on for the other commands in the page.
>
> I'm wondering if mayavi has been removed from sage, or whether its
> functionality is now part of the core packages?
>

No, it's there, and not in Sage - see http://www.sagemath.org/packages/experimental/
But probably the steps to use it need updating. You may also want to
search the lists - there are a fair number of threads about it.

I can't find the python framework one - can someone else help with the
status of that?

- kcrisman

ObsessiveMathsFreak

unread,
May 5, 2011, 3:40:55 PM5/5/11
to sage-support
_None_ of those experimental packages work on my system. The first
dependancy, wxPython-2.8.7.1.spkg give the following error (after
taking ages compiling)

In file included from ../src/gtk/gsockgtk.cpp:21:
../include/wx/gsocket.h:40: error: using typedef-name ‘GSocket’ after
‘class’
/usr/include/glib-2.0/gio/giotypes.h:132: error: ‘GSocket’ has a
previous declaration here
In file included from ../include/wx/gsocket.h:179,
from ../src/gtk/gsockgtk.cpp:21:
../include/wx/unix/gsockunx.h:40: error: using typedef-name ‘GSocket’
after ‘class’
/usr/include/glib-2.0/gio/giotypes.h:132: error: ‘GSocket’ has a
previous declaration here
../src/gtk/gsockgtk.cpp: In function ‘void _GSocket_GDK_Input(void*,
gint, GdkInputCondition)’:
../src/gtk/gsockgtk.cpp:34: error: ‘struct _GSocket’ has no member
named ‘Detected_Read’
../src/gtk/gsockgtk.cpp:36: error: ‘struct _GSocket’ has no member
named ‘Detected_Write’
../src/gtk/gsockgtk.cpp: In member function ‘virtual bool
GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket*)’:
../src/gtk/gsockgtk.cpp:56: error: ‘struct _GSocket’ has no member
named ‘m_gui_dependent’
../src/gtk/gsockgtk.cpp:57: error: ‘struct _GSocket’ has no member
named ‘m_gui_dependent’
../src/gtk/gsockgtk.cpp: In member function ‘virtual void
GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket*)’:
../src/gtk/gsockgtk.cpp:67: error: ‘struct _GSocket’ has no member
named ‘m_gui_dependent’
../src/gtk/gsockgtk.cpp: In member function ‘virtual void
GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket*,
GSocketEvent)’:
../src/gtk/gsockgtk.cpp:72: error: ‘struct _GSocket’ has no member
named ‘m_gui_dependent’
../src/gtk/gsockgtk.cpp:75: error: ‘struct _GSocket’ has no member
named ‘m_fd’
../src/gtk/gsockgtk.cpp:83: error: ‘struct _GSocket’ has no member
named ‘m_server’
../src/gtk/gsockgtk.cpp:90: error: ‘struct _GSocket’ has no member
named ‘m_fd’
../src/gtk/gsockgtk.cpp: In member function ‘virtual void
GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket*,
GSocketEvent)’:
../src/gtk/gsockgtk.cpp:98: error: ‘struct _GSocket’ has no member
named ‘m_gui_dependent’
../src/gtk/gsockgtk.cpp:108: error: ‘struct _GSocket’ has no member
named ‘m_server’
make: *** [monodll_gsockgtk.o] Error 1
Error building wx widgets.

real 1m54.899s
user 1m22.910s
sys 0m12.030s
sage: An error occurred while installing wxPython-2.8.7.1


The others give various different errors. For instance, I'm
encountering the "allow-hosts" error seen in this thread

http://groups.google.com/group/sage-support/browse_thread/thread/b6345c1fdebd43c4/6814b756af245381?lnk=gst&q=mayavi#6814b756af245381

But I don't even know if it was resolved in that thread or not.

IS there a way of getting any of these packages installed? Should I
update my sage installation to a newer version(currently 4.6.2)?

On May 5, 8:24 pm, kcrisman <kcris...@gmail.com> wrote:
> On May 5, 1:43 pm, ObsessiveMathsFreak <obsessivemathsfr...@gmail.com>
> wrote:
>
> > Thanks but, these methods are not wholly adequate for my purposes.
>
> > I have been investigating the mayavi python package and the examples
> > given herehttp://code.enthought.com/projects/mayavi/docs/development/html/mayav...
>
> > Apparently sagemath has support for mayavi, but when I follow the
> > instructions herehttp://www.sagemath.org/doc/numerical_sage/installation_osx.html
>
> > I get an error
>
> > sage: install_package("python-2.5.1-framework")
> > ValueError: There is no package name starting with 'python-2.5.1-
> > framework'.
>
> Looking at
>
> ./sage -optional
>
> I don't get these.  I think they are all experimental.
>
> > And so on for the other commands in the page.
>
> > I'm wondering if mayavi has been removed from sage, or whether its
> > functionality is now part of the core packages?
>
> No, it's there, and not in Sage - seehttp://www.sagemath.org/packages/experimental/

Jorge E. ´Sanchez Sanchez

unread,
May 5, 2011, 9:16:08 PM5/5/11
to sage-s...@googlegroups.com
I have made a few months ago the upgrade of mayavi experimental spkg to version 3.5,
I forgot the ticket info now (you can search it by the name ets-3.5.0-20101024.p0.spkg), to install it you have to
follow the guidelines in Jaap's page: http://sage.math.washington.edu/home/jsp/SPKGS/ETS, just instead of
the ets-3.2, I suggest you to use the one I mentioned before, besides mayavi it also includes also the Graph Canvas,
Scimath, SetupDocs and Traits Enthought Python parafernalia, I have been since then working with it inside sage,
without any problem.

Regards
Jorge

> Date: Thu, 5 May 2011 12:40:55 -0700
> Subject: [sage-support] Re: Plotting Functions of 3 variables
> From: obsessive...@gmail.com
> To: sage-s...@googlegroups.com
> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org

ObsessiveMathsFreak

unread,
May 6, 2011, 7:28:22 PM5/6/11
to sage-support
Well, I finally managed to install these packages, but mayavi seem
very flakey. Some example taken from the mayavi site work, others do
not.

Is there a list of updated sage examples for mayavi somehwhere? In
particular, are there any examples of plotting 3D data sets?



On May 6, 2:16 am, Jorge E. ´Sanchez Sanchez <hnr...@hotmail.com>
wrote:
> I have made a few months ago the upgrade of mayavi experimental spkg to version 3.5,
> I forgot the ticket info now (you can search it by the name ets-3.5.0-20101024.p0.spkg), to install it you have to
> follow the guidelines in Jaap's page:http://sage.math.washington.edu/home/jsp/SPKGS/ETS, just instead of
> the ets-3.2, I suggest you to use the one I mentioned before, besides mayavi it also includes also the Graph Canvas,
> Scimath, SetupDocs and Traits Enthought Python parafernalia, I have been since then working with it inside sage,
> without any problem.
>
> Regards
> Jorge
>
>
>
>
>
>
>
> > Date: Thu, 5 May 2011 12:40:55 -0700
> > Subject: [sage-support] Re: Plotting Functions of 3 variables
> > From: obsessivemathsfr...@gmail.com
> >http://groups.google.com/group/sage-support/browse_thread/thread/b634...

ObsessiveMathsFreak

unread,
May 6, 2011, 8:24:42 PM5/6/11
to sage-support
Actually, never mind all that. Mayavi simply rufuses to work properly
on my system and the documentation on sage integration is just too
scant.

I will just have to make do with implicit_plot3d and the like. I was
wondering if there is any way of integrating an implicit_plot3d plot
and a "slice" image plot of the function together as in this Mayavi
example

http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html#visualizing-volumetric-scalar-data

On May 7, 12:28 am, ObsessiveMathsFreak

Jorge E. ´Sanchez Sanchez

unread,
May 7, 2011, 11:56:14 AM5/7/11
to sage-s...@googlegroups.com
I'm so sorry to hear that, it's really a pity, perhaps the problem is that you are not quite familiar to mayavi,
I'm sending a code I modify to plot an interpolation of Mexico's City (data points file included) , and some nice pictures I got with mayavi.

Regards,
Jorge

> Date: Fri, 6 May 2011 17:24:42 -0700

> Subject: [sage-support] Re: Plotting Functions of 3 variables
mayavixmpls.tar.gz

ObsessiveMathsFreak

unread,
May 7, 2011, 6:00:39 PM5/7/11
to sage-support
I still can't get this code to work.

In lieu of any external solutions, I've tried to hack something
together in sage itself, using the standard 3d functionality.
Basically it involves drawing a series of implicit plot 3d countours,
then cutting into them with a small plane and drawing a 2d countour
based on that plane. I also found it useful to make the plotting
interactive. It's better than nothing I suppose. Here is some example
code

var('x y z u v')
f=lambda x,y,z: (y-x^2)+sin(z)
F=lambda X: f(X[0,0],X[1,0],X[2,0])

#3D level countours of function
p_slices=sum([ implicit_plot3d(f(x,y,z)==w, (x, -3, 3), (y, -3,3), (z,
-3,3),opacity=0.1,color="green") for w in [-1,-0.5,..,1] ])

@interact
def _(phi = slider(-pi/2,pi/2,pi/20,default=pi/2),theta = slider(-
pi,pi,pi/20,default=0),\

x0=slider(-1,1,0.1,default=0),y0=slider(-1,1,0.1,default=0),z0=slider(-1,1,0.1,default=0),
\
R=slider(0.1,10,0.1,default=1)):


p=matrix([x0,y0,z0]).transpose()
#center of 2D plot
e1=matrix([cos(theta)*sin(phi), sin(theta)*sin(phi), -
cos(phi)]).transpose() #u axis of 2D plot
e2=matrix([-sin(theta), cos(theta),
0]).transpose() #v axis of 2D plot

V=lambda u,v: p
+u*e1+v*e2 #map
u,v to x,y,z

#pcon=density_plot(F(V(u,v)),(u,-R,R),(v,-R,R),cmap="gray")
pcon=contour_plot(F(V(u,v)),(u,-R,R),(v,-
R,R),cmap="gray",fill=false)
pcon.set_aspect_ratio(1)

pplane=parametric_plot3d( [V(u,v)[0,0],V(u,v)[1,0],V(u,v)[2,0]],
(u,-R,R),(v,-R,R),opacity=0.6,color="red") #location of plane in 3D

(pplane+p_slices).show(aspect_ratio=[1,1,1])
show(pcon)


However, once again I find myself frustrated by plot3ds refusal to
allow itself to be position beside other plots. Consequently, the
plots will not all fit in one screen for me.

The other major problem is the complete and total lack of colour
coding of plots. Does anyone know of a way to control the colour
output of 2D countour plots or density plots, so as to make the colour
scheme consisten across both the 2D and 3D plots (or at least to stop
the colour scheme rescaling every time the 2D square plane segment is
moved.

On May 7, 4:56 pm, Jorge E. ´Sanchez Sanchez <hnr...@hotmail.com>
wrote:
> I'm so sorry to hear that, it's really a pity, perhaps the problem is that you are not quite familiar to mayavi,
> I'm sending a code I modify to plot an interpolation of Mexico's City (data points file included) , and some nice pictures I got with mayavi.
>
> Regards,
> Jorge
>
>
>
>
>
>
>
> > Date: Fri, 6 May 2011 17:24:42 -0700
> > Subject: [sage-support] Re: Plotting Functions of 3 variables
> > From: obsessivemathsfr...@gmail.com
> > To: sage-s...@googlegroups.com
>
> > Actually, never mind all that. Mayavi simply rufuses to work properly
> > on my system and the documentation on sage integration is just too
> > scant.
>
> > I will just have to make do with implicit_plot3d and the like. I was
> > wondering if there is any way of integrating an implicit_plot3d plot
> > and a "slice" image plot of the function together as in this Mayavi
> > example
>
> >http://code.enthought.com/projects/mayavi/docs/development/html/mayav...
>  mayavixmpls.tar.gz
> 1032KViewDownload

Jason Grout

unread,
May 7, 2011, 7:01:22 PM5/7/11
to sage-s...@googlegroups.com

Yes, this has frustrated me too.


>
> The other major problem is the complete and total lack of colour
> coding of plots. Does anyone know of a way to control the colour
> output of 2D countour plots or density plots, so as to make the colour
> scheme consisten across both the 2D and 3D plots (or at least to stop
> the colour scheme rescaling every time the 2D square plane segment is
> moved.


We haven't wrapped the equivalent of the vmin and vmax parameters from
the matrix_plot yet. You can use the matrix_plot to specify hard limits
for the top and bottom colors of a colormap:

http://www.sagemath.org/doc/reference/sage/plot/matrix_plot.html

It should be straightforward to add the same functionality to contour
and density plots. If you need it right away, you might use matplotlib
in the meantime, which is what we use underneath things to do 2d plots.

Jason

Reply all
Reply to author
Forward
0 new messages