ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 10 Jan 2017 14:02:44 +0100use a colormap for implicit_plot3dhttps://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/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 12212Thu, 19 Aug 2010 07:26:08 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/Answer by FrédéricC for <p>Here's a question I asked a while ago on sage-support, but never got answered, so I'll try it again here:</p>
<p>I've been trying to figure out how to plot a surface using
<code>implicit_plot3d</code>, and color it according to a particular color map. I
see that <code>implicit_plot3d</code> passes its arguments to</p>
<p><code>sage.plot.plot3d.implicit_plot3d.ImplicitSurface</code></p>
<p>but I can't figure out more than this . . . when I try</p>
<p><code>sage: sage.plot.plot3d.implicit_plot3d.ImplicitSurface??</code></p>
<p>I get</p>
<p><code>Error getting source: could not find class definition</code></p>
<p>and then a few more unhelpful details.</p>
<p>It seems like the two possible viewers are jMol and Tachyon, but I
can't tell whether either of these support color maps.</p>
<p>Any ideas where else I should be looking? </p>
<p>Update: This is now Trac ticket 12212</p>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=36248#post-id-36248This is now possible as follows.
var('x,y,z')
cm = colormaps.autumn
f = cos(z*4).function(x,y,z)
implicit_plot3d(x^2-y^2*z == 0,(x,-4,4),(y,-4,4),(z,-4,4), color=(f,cm))
where cm is a chosen colormap among those available.
Beware that the coloring function f must take values between 0 and 1.Tue, 10 Jan 2017 14:02:44 +0100https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=36248#post-id-36248Answer by kcrisman for <p>Here's a question I asked a while ago on sage-support, but never got answered, so I'll try it again here:</p>
<p>I've been trying to figure out how to plot a surface using
<code>implicit_plot3d</code>, and color it according to a particular color map. I
see that <code>implicit_plot3d</code> passes its arguments to</p>
<p><code>sage.plot.plot3d.implicit_plot3d.ImplicitSurface</code></p>
<p>but I can't figure out more than this . . . when I try</p>
<p><code>sage: sage.plot.plot3d.implicit_plot3d.ImplicitSurface??</code></p>
<p>I get</p>
<p><code>Error getting source: could not find class definition</code></p>
<p>and then a few more unhelpful details.</p>
<p>It seems like the two possible viewers are jMol and Tachyon, but I
can't tell whether either of these support color maps.</p>
<p>Any ideas where else I should be looking? </p>
<p>Update: This is now Trac ticket 12212</p>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=11508#post-id-11508Thanks 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.Sat, 21 Aug 2010 22:37:47 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=11508#post-id-11508Comment by niles for <p>Thanks for the followup, Niles. This is a really annoying part of some things that are defined in <code>.pyx</code> files - <code>ImplicitSurface</code> is a <code>cdef</code> (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.</p>
<p>It turns out that <code>**kwds</code> (which would include color) end up in the init of <code>IndexFaceSet</code> which sends it to <code>PrimitiveObject</code> in <code>plot3d/base.pyx</code> which includes </p>
<blockquote>
<pre><code> else:
self.texture = Texture(kwds)
</code></pre>
</blockquote>
<p>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 <code>Texture</code>. And I think that following this the bug is that <code>parse_color</code> does not take a multiplicity of colors.</p>
<p>This still doesn't answer how to GET the colormap, but hopefully tracks down some of why it isn't working.</p>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22939#post-id-22939Thanks for the help . . . I'll keep digging :)Sat, 21 Aug 2010 23:35:24 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22939#post-id-22939Comment by kcrisman for <p>Thanks for the followup, Niles. This is a really annoying part of some things that are defined in <code>.pyx</code> files - <code>ImplicitSurface</code> is a <code>cdef</code> (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.</p>
<p>It turns out that <code>**kwds</code> (which would include color) end up in the init of <code>IndexFaceSet</code> which sends it to <code>PrimitiveObject</code> in <code>plot3d/base.pyx</code> which includes </p>
<blockquote>
<pre><code> else:
self.texture = Texture(kwds)
</code></pre>
</blockquote>
<p>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 <code>Texture</code>. And I think that following this the bug is that <code>parse_color</code> does not take a multiplicity of colors.</p>
<p>This still doesn't answer how to GET the colormap, but hopefully tracks down some of why it isn't working.</p>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22941#post-id-22941But 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`?Sat, 21 Aug 2010 22:44:22 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22941#post-id-22941Comment by kcrisman for <p>Thanks for the followup, Niles. This is a really annoying part of some things that are defined in <code>.pyx</code> files - <code>ImplicitSurface</code> is a <code>cdef</code> (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.</p>
<p>It turns out that <code>**kwds</code> (which would include color) end up in the init of <code>IndexFaceSet</code> which sends it to <code>PrimitiveObject</code> in <code>plot3d/base.pyx</code> which includes </p>
<blockquote>
<pre><code> else:
self.texture = Texture(kwds)
</code></pre>
</blockquote>
<p>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 <code>Texture</code>. And I think that following this the bug is that <code>parse_color</code> does not take a multiplicity of colors.</p>
<p>This still doesn't answer how to GET the colormap, but hopefully tracks down some of why it isn't working.</p>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22942#post-id-22942And `plot3d` seems to also eventually end up at `IndexFaceSet` via `ParametricSurface` anyway, so this doesn't resolve the mystery at all, perhaps.Sat, 21 Aug 2010 22:41:20 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22942#post-id-22942Answer by Jason Grout for <p>Here's a question I asked a while ago on sage-support, but never got answered, so I'll try it again here:</p>
<p>I've been trying to figure out how to plot a surface using
<code>implicit_plot3d</code>, and color it according to a particular color map. I
see that <code>implicit_plot3d</code> passes its arguments to</p>
<p><code>sage.plot.plot3d.implicit_plot3d.ImplicitSurface</code></p>
<p>but I can't figure out more than this . . . when I try</p>
<p><code>sage: sage.plot.plot3d.implicit_plot3d.ImplicitSurface??</code></p>
<p>I get</p>
<p><code>Error getting source: could not find class definition</code></p>
<p>and then a few more unhelpful details.</p>
<p>It seems like the two possible viewers are jMol and Tachyon, but I
can't tell whether either of these support color maps.</p>
<p>Any ideas where else I should be looking? </p>
<p>Update: This is now Trac ticket 12212</p>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=11472#post-id-11472I 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()
Thu, 19 Aug 2010 23:23:34 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=11472#post-id-11472Comment by William Stein for <p>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:</p>
<pre><code>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])
</code></pre>
<p>You can see the available colormaps by doing </p>
<pre><code>sage: colormaps.keys()
</code></pre>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22994#post-id-22994You can't comment until you get enough karma.Fri, 20 Aug 2010 01:26:09 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22994#post-id-22994Comment by Mitesh Patel for <p>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:</p>
<pre><code>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])
</code></pre>
<p>You can see the available colormaps by doing </p>
<pre><code>sage: colormaps.keys()
</code></pre>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22938#post-id-22938How hard would it be to implement colorbars for 3D plots?Sun, 22 Aug 2010 04:45:45 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=22938#post-id-22938Answer by Harald Schilly for <p>Here's a question I asked a while ago on sage-support, but never got answered, so I'll try it again here:</p>
<p>I've been trying to figure out how to plot a surface using
<code>implicit_plot3d</code>, and color it according to a particular color map. I
see that <code>implicit_plot3d</code> passes its arguments to</p>
<p><code>sage.plot.plot3d.implicit_plot3d.ImplicitSurface</code></p>
<p>but I can't figure out more than this . . . when I try</p>
<p><code>sage: sage.plot.plot3d.implicit_plot3d.ImplicitSurface??</code></p>
<p>I get</p>
<p><code>Error getting source: could not find class definition</code></p>
<p>and then a few more unhelpful details.</p>
<p>It seems like the two possible viewers are jMol and Tachyon, but I
can't tell whether either of these support color maps.</p>
<p>Any ideas where else I should be looking? </p>
<p>Update: This is now Trac ticket 12212</p>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=11410#post-id-11410I 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]) Thu, 19 Aug 2010 09:15:50 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=11410#post-id-11410Comment by William Stein for <p>I have once managed to get plot3d use a colormap, see here:</p>
<pre><code>import matplotlib.cm
matplotlib.cm.datad.keys()
</code></pre>
<p>lists you the colormaps, you can also create your own. Note, the cmsel list in the following snippet defines the actual colors.</p>
<pre><code>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])
</code></pre>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=23068#post-id-23068OMFG this is *awesome*. I had no idea this was possible in Sage. Thu, 19 Aug 2010 13:54:56 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=23068#post-id-23068Answer by niles for <p>Here's a question I asked a while ago on sage-support, but never got answered, so I'll try it again here:</p>
<p>I've been trying to figure out how to plot a surface using
<code>implicit_plot3d</code>, and color it according to a particular color map. I
see that <code>implicit_plot3d</code> passes its arguments to</p>
<p><code>sage.plot.plot3d.implicit_plot3d.ImplicitSurface</code></p>
<p>but I can't figure out more than this . . . when I try</p>
<p><code>sage: sage.plot.plot3d.implicit_plot3d.ImplicitSurface??</code></p>
<p>I get</p>
<p><code>Error getting source: could not find class definition</code></p>
<p>and then a few more unhelpful details.</p>
<p>It seems like the two possible viewers are jMol and Tachyon, but I
can't tell whether either of these support color maps.</p>
<p>Any ideas where else I should be looking? </p>
<p>Update: This is now Trac ticket 12212</p>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=11432#post-id-11432Apologies 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
Fri, 20 Aug 2010 17:29:51 +0200https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?answer=11432#post-id-11432Comment by jvkersch for <p>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 <code>implicit_plot3d</code> (works fine for <code>plot3d</code>). And now I am remembering why I wanted to see where <code>implicit_plot3d</code> sends its arguments: it seems to be handling <code>color</code> in a different way than <code>plot3d</code>.</p>
<p>so the following is fine:</p>
<pre><code>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')
</code></pre>
<p>but this is not:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=20670#post-id-20670Great, thanks! I was sort of hoping that you had implemented this for your Hopf fibration video :)Wed, 21 Dec 2011 12:08:26 +0100https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=20670#post-id-20670Comment by jvkersch for <p>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 <code>implicit_plot3d</code> (works fine for <code>plot3d</code>). And now I am remembering why I wanted to see where <code>implicit_plot3d</code> sends its arguments: it seems to be handling <code>color</code> in a different way than <code>plot3d</code>.</p>
<p>so the following is fine:</p>
<pre><code>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')
</code></pre>
<p>but this is not:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=20677#post-id-20677Niles, 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?Tue, 20 Dec 2011 17:16:04 +0100https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=20677#post-id-20677Comment by niles for <p>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 <code>implicit_plot3d</code> (works fine for <code>plot3d</code>). And now I am remembering why I wanted to see where <code>implicit_plot3d</code> sends its arguments: it seems to be handling <code>color</code> in a different way than <code>plot3d</code>.</p>
<p>so the following is fine:</p>
<pre><code>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')
</code></pre>
<p>but this is not:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=20673#post-id-20673No, 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 .Wed, 21 Dec 2011 08:30:13 +0100https://ask.sagemath.org/question/7581/use-a-colormap-for-implicit_plot3d/?comment=20673#post-id-20673