use a colormap for implicit_plot3d

i like this post (click again to cancel)
1
i dont like this post (click again to cancel)

Here's a question I asked a while ago on sage-support, but never got answered, so I'll try it again here:

I've been trying to figure out how to plot a surface using implicit_plot3d, and color it according to a particular color map. I see that implicit_plot3d passes its arguments to

sage.plot.plot3d.implicit_plot3d.ImplicitSurface

but I can't figure out more than this . . . when I try

sage: sage.plot.plot3d.implicit_plot3d.ImplicitSurface??

I get

Error getting source: could not find class definition

and then a few more unhelpful details.

It seems like the two possible viewers are jMol and Tachyon, but I can't tell whether either of these support color maps.

Any ideas where else I should be looking?

Update: This is now Trac ticket 12212

asked Aug 19 '10

niles gravatar image niles
3354 5 38 92
http://nilesjohnson.net/

updated May 10 '12

4 Answers:

i like this answer (click again to cancel)
2
i dont like this answer (click again to cancel)

I have once managed to get plot3d use a colormap, see here:

import matplotlib.cm
matplotlib.cm.datad.keys()

lists you the colormaps, you can also create your own. Note, the cmsel list in the following snippet defines the actual colors.

var('r v')
cmsel = [matplotlib.cm.get_cmap('autumn')(_) for _ in sxrange(0,1,0.05)]
p = plot3d(0.2*(r**2 + v**2) + cos(2*r)*sin(2*v),(r,-2,2), (v,-2,2), adaptive=True, color=cmsel, plot_points=10, opacity=0.9)
p2 = sphere((0,0,0),1,color='black',opacity=0.5)
(p+p2).show(aspect_ratio=(1,1,1), figsize=[7,3])
link

posted Aug 19 '10

schilly gravatar image schilly
257 4 5 10
OMFG this is *awesome*. I had no idea this was possible in Sage. William Stein (Aug 19 '10)
i like this answer (click again to cancel)
2
i dont like this answer (click again to cancel)

I don't know how to comment on another answer, so I'll just post my comment here. You don't have to import matplotlib colormaps, since they are already imported in the sage colormaps object:

sage: var('r v')
sage: cmsel = [colormaps['autumn'](i) for i in sxrange(0,1,0.05)]
sage: p = plot3d(0.2*(r**2 + v**2) + cos(2*r)*sin(2*v),(r,-2,2), (v,-2,2), adaptive=True, color=cmsel, plot_points=10, opacity=0.9)
sage: p2 = sphere((0,0,0),1,color='black',opacity=0.5)
sage: (p+p2).show(aspect_ratio=(1,1,1), figsize=[7,3])

You can see the available colormaps by doing

sage: colormaps.keys()
link

posted Aug 19 '10

Jason Grout gravatar image Jason Grout
3185 7 27 71

updated Aug 20 '10

You can't comment until you get enough karma. William Stein (Aug 19 '10)
How hard would it be to implement colorbars for 3D plots? Mitesh Patel (Aug 22 '10)
i like this answer (click again to cancel)
1
i dont like this answer (click again to cancel)

Thanks for the followup, Niles. This is a really annoying part of some things that are defined in .pyx files - ImplicitSurface is a cdef (Cython) function, and these don't always work well with introspection in my experience. Weirdly, I don't get the traceback you do in the notebook.

It turns out that **kwds (which would include color) end up in the init of IndexFaceSet which sends it to PrimitiveObject in plot3d/base.pyx which includes

    else:
        self.texture = Texture(kwds)

And if you look at THAT you will see that that just goes through a whole slew of possible things the input could be to Texture. And I think that following this the bug is that parse_color does not take a multiplicity of colors.

This still doesn't answer how to GET the colormap, but hopefully tracks down some of why it isn't working.

link

posted Aug 21 '10

kcrisman gravatar image kcrisman
6639 13 66 150
And `plot3d` seems to also eventually end up at `IndexFaceSet` via `ParametricSurface` anyway, so this doesn't resolve the mystery at all, perhaps. kcrisman (Aug 21 '10)
But I did finally get the error message, and indeed the problem is in `parse_color`, where there isn't an `else` for `info` something other than a string or `Color`. But the fix would have to be in `Texture` or something. Why *does* it work with `plot3d`? kcrisman (Aug 21 '10)
Thanks for the help . . . I'll keep digging :) niles (Aug 21 '10)
i like this answer (click again to cancel)
0
i dont like this answer (click again to cancel)

Apologies for the delay; I assumed the answers from schilly and jason-grout would work (sorry!) but I got around to testing it today and it doesn't work for implicit_plot3d (works fine for plot3d). And now I am remembering why I wanted to see where implicit_plot3d sends its arguments: it seems to be handling color in a different way than plot3d.

so the following is fine:

sage: var('x,y,z')
sage: implicit_plot3d(x^2+y^2+z^2==4, (x, -3, 3), (y, -3,3), (z, -3,3), color='red')

but this is not:

sage: cmsel = [colormaps['autumn'](i) for i in sxrange(0,1,0.05)]
sage: var('x,y,z')
sage: implicit_plot3d(x^2+y^2+z^2==4, (x, -3, 3), (y, -3,3), (z, -3,3), color=cmsel)
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (49, 0))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: 'NoneType' object is not iterable
link

posted Aug 20 '10

niles gravatar image niles
3354 5 38 92
http://nilesjohnson.net/

Niles, did you ever get this to work? I need exactly the same functionality, but for parametric_plot3d, maybe you can open a ticket for this?

jvkersch (Dec 20 '11)

No, I never did get it working. Instead I redesigned my project so that I didn't need it . . . the ticket is http://trac.sagemath.org/sage_trac/ticket/12212

niles (Dec 21 '11)

Great, thanks! I was sort of hoping that you had implemented this for your Hopf fibration video :)

jvkersch (Dec 21 '11)

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
[hide preview]

Question tools

Tags:

Stats:

Asked: Aug 19 '10

Seen: 408 times

Last updated: Dec 21 '11

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.