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 15:46:23 +0100How to color a 3d plot by z-level?https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/I'd like to color a 3d plot based on z-level. I think this is easy to do in Maple or Mathematica, but I've been searching the web for help on doing this in Sage and can't find anything to help with plot3d or implicit_plot3d.
Here's the implicit_plot3d I'm using.
var('x,y,z')
implicit_plot3d(x^2-y^2*z == 0,(x,-4,4),(y,-4,4),(z,-4,4)).show(mesh=True)
Also, are there color maps in Sage that produce plots with colors and lighting similar to the default in Mathematica?
Wed, 09 May 2012 23:27:40 +0200https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/Comment by kcrisman for <p>I'd like to color a 3d plot based on z-level. I think this is easy to do in Maple or Mathematica, but I've been searching the web for help on doing this in Sage and can't find anything to help with plot3d or implicit_plot3d.</p>
<p>Here's the implicit_plot3d I'm using.</p>
<pre><code>var('x,y,z')
implicit_plot3d(x^2-y^2*z == 0,(x,-4,4),(y,-4,4),(z,-4,4)).show(mesh=True)
</code></pre>
<p>Also, are there color maps in Sage that produce plots with colors and lighting similar to the default in Mathematica?</p>
https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=18169#post-id-18169Niles also eventually opened up the possibly relevant http://trac.sagemath.org/sage_trac/ticket/12212Sun, 24 Feb 2013 20:30:19 +0100https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=18169#post-id-18169Answer by FrédéricC for <p>I'd like to color a 3d plot based on z-level. I think this is easy to do in Maple or Mathematica, but I've been searching the web for help on doing this in Sage and can't find anything to help with plot3d or implicit_plot3d.</p>
<p>Here's the implicit_plot3d I'm using.</p>
<pre><code>var('x,y,z')
implicit_plot3d(x^2-y^2*z == 0,(x,-4,4),(y,-4,4),(z,-4,4)).show(mesh=True)
</code></pre>
<p>Also, are there color maps in Sage that produce plots with colors and lighting similar to the default in Mathematica?</p>
https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?answer=36246#post-id-36246This is now possible as follows.
var('x,y,z')
cm = colormaps.autumn
f = (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:00:38 +0100https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?answer=36246#post-id-36246Comment by calc314 for <p>This is now possible as follows.</p>
<pre><code>var('x,y,z')
cm = colormaps.autumn
f = (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))
</code></pre>
<p>where cm is a chosen colormap among those available.
Beware that the coloring function f must take values between 0 and 1.</p>
https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=36251#post-id-36251Nice! Thanks!Tue, 10 Jan 2017 14:44:27 +0100https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=36251#post-id-36251Comment by kcrisman for <p>This is now possible as follows.</p>
<pre><code>var('x,y,z')
cm = colormaps.autumn
f = (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))
</code></pre>
<p>where cm is a chosen colormap among those available.
Beware that the coloring function f must take values between 0 and 1.</p>
https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=36254#post-id-36254(In fact, apparently it's been available for well over a year! Nice necro-post, Frédéric!)Tue, 10 Jan 2017 15:46:23 +0100https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=36254#post-id-36254Answer by niles for <p>I'd like to color a 3d plot based on z-level. I think this is easy to do in Maple or Mathematica, but I've been searching the web for help on doing this in Sage and can't find anything to help with plot3d or implicit_plot3d.</p>
<p>Here's the implicit_plot3d I'm using.</p>
<pre><code>var('x,y,z')
implicit_plot3d(x^2-y^2*z == 0,(x,-4,4),(y,-4,4),(z,-4,4)).show(mesh=True)
</code></pre>
<p>Also, are there color maps in Sage that produce plots with colors and lighting similar to the default in Mathematica?</p>
https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?answer=13563#post-id-13563I asked a similar question about using a [colormap for implicit_plot3d](http://ask.sagemath.org/question/17/use-a-colormap-for-implicit_plot3d) almost a year ago, and it is still open. @schilly *did* show how to use matplotlib colormaps for `plot3d` though, and it's pretty cool:
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)
sage: p.show(aspect_ratio=(1,1,1), figsize=[7,3])
![image description](/upfiles/13366565749981129.png)
And, as @jason-grout said, you can see all of the available colormaps with `colormaps.keys()`.
If you do figure out some way to use colormaps with *implicit* 3d plots, I would love to hear about it!
Thu, 10 May 2012 10:32:53 +0200https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?answer=13563#post-id-13563Comment by calc314 for <p>I asked a similar question about using a <a href="http://ask.sagemath.org/question/17/use-a-colormap-for-implicit_plot3d">colormap for implicit_plot3d</a> almost a year ago, and it is still open. <a href="/users/4/schilly/">@schilly</a> <em>did</em> show how to use matplotlib colormaps for <code>plot3d</code> though, and it's pretty cool:</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)
sage: p.show(aspect_ratio=(1,1,1), figsize=[7,3])
</code></pre>
<p><img alt="image description" src="/upfiles/13366565749981129.png"/></p>
<p>And, as @jason-grout said, you can see all of the available colormaps with <code>colormaps.keys()</code>.</p>
<p>If you do figure out some way to use colormaps with <em>implicit</em> 3d plots, I would love to hear about it!</p>
https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=19797#post-id-19797Wow! That does a great job. It would still be great to have it built into the plot3d and implicit_plot3d commands so that it is more friendly and intuitive.Thu, 10 May 2012 11:30:11 +0200https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=19797#post-id-19797Answer by Shashank for <p>I'd like to color a 3d plot based on z-level. I think this is easy to do in Maple or Mathematica, but I've been searching the web for help on doing this in Sage and can't find anything to help with plot3d or implicit_plot3d.</p>
<p>Here's the implicit_plot3d I'm using.</p>
<pre><code>var('x,y,z')
implicit_plot3d(x^2-y^2*z == 0,(x,-4,4),(y,-4,4),(z,-4,4)).show(mesh=True)
</code></pre>
<p>Also, are there color maps in Sage that produce plots with colors and lighting similar to the default in Mathematica?</p>
https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?answer=13560#post-id-13560Have a look at this. Is this what you are looking for? You can run matplotlib scripts in sage.
http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo.htmlThu, 10 May 2012 00:21:01 +0200https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?answer=13560#post-id-13560Comment by calc314 for <p>Have a look at this. Is this what you are looking for? You can run matplotlib scripts in sage. </p>
<p><a href="http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo.html">http://matplotlib.sourceforge.net/exa...</a></p>
https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=19798#post-id-19798That code does produce some good looking plots. However, it is too involved for me to use with calculus students without writing some sort of wrapper for it. Also, I definitely cannot take this as a solution to a colleague who is not an expert programmer. Are there simpler options to get this result?Thu, 10 May 2012 10:26:40 +0200https://ask.sagemath.org/question/8964/how-to-color-a-3d-plot-by-z-level/?comment=19798#post-id-19798