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.Fri, 25 Nov 2011 14:13:52 -0600Overlaying plots in a specific orderhttp://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/I've got two plots: one is a density plot, one is a list plot. I'd like to combine the plots in such a way that the list plot is overlaid on top of the density plot, so the dots or visible. No matter what I do, it seems that the density plot is always in front and list plot is in back.
This isn't my code, but it appears to have the same behavior:
sage: x_coords = [cos(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: y_coords = [sin(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: h1 = list_plot(zip(x_coords, y_coords))
sage: x,y = var('x,y')
sage: h2 = density_plot(sin(x)*sin(y), (x, -2, 2), (y, -2, 2), cmap='jet')
sage: h1+h2
How can I make the list plot on top (or in general, specify the order of any combined plots)? Thu, 24 Nov 2011 11:51:40 -0600http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/Answer by DSM for <p>I've got two plots: one is a density plot, one is a list plot. I'd like to combine the plots in such a way that the list plot is overlaid on top of the density plot, so the dots or visible. No matter what I do, it seems that the density plot is always in front and list plot is in back.</p>
<p>This isn't my code, but it appears to have the same behavior:</p>
<pre><code>sage: x_coords = [cos(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: y_coords = [sin(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: h1 = list_plot(zip(x_coords, y_coords))
sage: x,y = var('x,y')
sage: h2 = density_plot(sin(x)*sin(y), (x, -2, 2), (y, -2, 2), cmap='jet')
sage: h1+h2
</code></pre>
<p>How can I make the list plot on top (or in general, specify the order of any combined plots)? </p>
http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?answer=12936#post-id-12936You might be interested in the answers to [this question about zordering](http://ask.sagemath.org/question/268/automatic-zordering-with-plots)..
You can choose how to zorder (i.e. choose the "depth" of an image) by calling set_zorder on the resulting graphics objects [not on the plots]. For example:
sage: x_coords = [cos(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: y_coords = [sin(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: h1 = list_plot(zip(x_coords, y_coords))
sage: x,y = var('x,y')
sage: h2 = density_plot(sin(x)*sin(y), (x, -2, 2), (y, -2, 2), cmap='jet')
sage: h = h1+h2
sage: list(h)
[Point set defined by 315 point(s), DensityPlot defined by a 25 x 25 data grid]
sage: h[0].set_zorder(10)
sage: show(h)
![image description](/upfiles/13221809999536048.png)
But I have to admit that it doesn't look like all the objects you might want to support zordering the way they should:
sage: h[1].set_zorder(20)
sage: show(h)
verbose 0 (138: primitive.py, options) WARNING: Ignoring option 'zorder'=20
verbose 0 (138: primitive.py, options)
The allowed options for DensityPlot defined by a 25 x 25 data grid are:
cmap the name of a predefined colormap,
a list of colors or an instance of a
matplotlib Colormap. Type: import matplotlib.cm; matplotlib.cm.datad.keys()
for available colormap names.
interpolation What interpolation method to use
plot_points How many points to use for plotting precision
This is an inconvenience because it's not pretty to force it via the underlying matplotlib objects, but I might be missing something.Thu, 24 Nov 2011 12:33:24 -0600http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?answer=12936#post-id-12936Comment by kcrisman for <p>You might be interested in the answers to <a href="http://ask.sagemath.org/question/268/automatic-zordering-with-plots">this question about zordering</a>..</p>
<p>You can choose how to zorder (i.e. choose the "depth" of an image) by calling set_zorder on the resulting graphics objects [not on the plots]. For example:</p>
<pre><code>sage: x_coords = [cos(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: y_coords = [sin(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: h1 = list_plot(zip(x_coords, y_coords))
sage: x,y = var('x,y')
sage: h2 = density_plot(sin(x)*sin(y), (x, -2, 2), (y, -2, 2), cmap='jet')
sage: h = h1+h2
sage: list(h)
[Point set defined by 315 point(s), DensityPlot defined by a 25 x 25 data grid]
sage: h[0].set_zorder(10)
sage: show(h)
</code></pre>
<p><img alt="image description" src="/upfiles/13221809999536048.png"/></p>
<p>But I have to admit that it doesn't look like all the objects you might want to support zordering the way they should:</p>
<pre><code>sage: h[1].set_zorder(20)
sage: show(h)
verbose 0 (138: primitive.py, options) WARNING: Ignoring option 'zorder'=20
verbose 0 (138: primitive.py, options)
The allowed options for DensityPlot defined by a 25 x 25 data grid are:
cmap the name of a predefined colormap,
a list of colors or an instance of a
matplotlib Colormap. Type: import matplotlib.cm; matplotlib.cm.datad.keys()
for available colormap names.
interpolation What interpolation method to use
plot_points How many points to use for plotting precision
</code></pre>
<p>This is an inconvenience because it's not pretty to force it via the underlying matplotlib objects, but I might be missing something.</p>
http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?comment=20823#post-id-20823@Jason Grout: Good idea - if you open a ticket, cc: me. Fri, 25 Nov 2011 14:12:57 -0600http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?comment=20823#post-id-20823Comment by Jason for <p>You might be interested in the answers to <a href="http://ask.sagemath.org/question/268/automatic-zordering-with-plots">this question about zordering</a>..</p>
<p>You can choose how to zorder (i.e. choose the "depth" of an image) by calling set_zorder on the resulting graphics objects [not on the plots]. For example:</p>
<pre><code>sage: x_coords = [cos(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: y_coords = [sin(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: h1 = list_plot(zip(x_coords, y_coords))
sage: x,y = var('x,y')
sage: h2 = density_plot(sin(x)*sin(y), (x, -2, 2), (y, -2, 2), cmap='jet')
sage: h = h1+h2
sage: list(h)
[Point set defined by 315 point(s), DensityPlot defined by a 25 x 25 data grid]
sage: h[0].set_zorder(10)
sage: show(h)
</code></pre>
<p><img alt="image description" src="/upfiles/13221809999536048.png"/></p>
<p>But I have to admit that it doesn't look like all the objects you might want to support zordering the way they should:</p>
<pre><code>sage: h[1].set_zorder(20)
sage: show(h)
verbose 0 (138: primitive.py, options) WARNING: Ignoring option 'zorder'=20
verbose 0 (138: primitive.py, options)
The allowed options for DensityPlot defined by a 25 x 25 data grid are:
cmap the name of a predefined colormap,
a list of colors or an instance of a
matplotlib Colormap. Type: import matplotlib.cm; matplotlib.cm.datad.keys()
for available colormap names.
interpolation What interpolation method to use
plot_points How many points to use for plotting precision
</code></pre>
<p>This is an inconvenience because it's not pretty to force it via the underlying matplotlib objects, but I might be missing something.</p>
http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?comment=20827#post-id-20827Thanks, that works. Why can't I do something like this: h1.set_zorder(10)+h2.set_zorder(11) ? I notice that h1 and h[0] are different types, that seems a little unintuitive.Thu, 24 Nov 2011 13:57:24 -0600http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?comment=20827#post-id-20827Comment by kcrisman for <p>You might be interested in the answers to <a href="http://ask.sagemath.org/question/268/automatic-zordering-with-plots">this question about zordering</a>..</p>
<p>You can choose how to zorder (i.e. choose the "depth" of an image) by calling set_zorder on the resulting graphics objects [not on the plots]. For example:</p>
<pre><code>sage: x_coords = [cos(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: y_coords = [sin(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: h1 = list_plot(zip(x_coords, y_coords))
sage: x,y = var('x,y')
sage: h2 = density_plot(sin(x)*sin(y), (x, -2, 2), (y, -2, 2), cmap='jet')
sage: h = h1+h2
sage: list(h)
[Point set defined by 315 point(s), DensityPlot defined by a 25 x 25 data grid]
sage: h[0].set_zorder(10)
sage: show(h)
</code></pre>
<p><img alt="image description" src="/upfiles/13221809999536048.png"/></p>
<p>But I have to admit that it doesn't look like all the objects you might want to support zordering the way they should:</p>
<pre><code>sage: h[1].set_zorder(20)
sage: show(h)
verbose 0 (138: primitive.py, options) WARNING: Ignoring option 'zorder'=20
verbose 0 (138: primitive.py, options)
The allowed options for DensityPlot defined by a 25 x 25 data grid are:
cmap the name of a predefined colormap,
a list of colors or an instance of a
matplotlib Colormap. Type: import matplotlib.cm; matplotlib.cm.datad.keys()
for available colormap names.
interpolation What interpolation method to use
plot_points How many points to use for plotting precision
</code></pre>
<p>This is an inconvenience because it's not pretty to force it via the underlying matplotlib objects, but I might be missing something.</p>
http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?comment=20822#post-id-20822As a general comment to any interested readers, see Trac tickets #6249 and #3251 for places where improving this (in general, not necessarily for density plots) is mentioned.Fri, 25 Nov 2011 14:13:52 -0600http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?comment=20822#post-id-20822Comment by Jason Grout for <p>You might be interested in the answers to <a href="http://ask.sagemath.org/question/268/automatic-zordering-with-plots">this question about zordering</a>..</p>
<p>You can choose how to zorder (i.e. choose the "depth" of an image) by calling set_zorder on the resulting graphics objects [not on the plots]. For example:</p>
<pre><code>sage: x_coords = [cos(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: y_coords = [sin(t)^3 for t in srange(0, 2*pi, 0.02)]
sage: h1 = list_plot(zip(x_coords, y_coords))
sage: x,y = var('x,y')
sage: h2 = density_plot(sin(x)*sin(y), (x, -2, 2), (y, -2, 2), cmap='jet')
sage: h = h1+h2
sage: list(h)
[Point set defined by 315 point(s), DensityPlot defined by a 25 x 25 data grid]
sage: h[0].set_zorder(10)
sage: show(h)
</code></pre>
<p><img alt="image description" src="/upfiles/13221809999536048.png"/></p>
<p>But I have to admit that it doesn't look like all the objects you might want to support zordering the way they should:</p>
<pre><code>sage: h[1].set_zorder(20)
sage: show(h)
verbose 0 (138: primitive.py, options) WARNING: Ignoring option 'zorder'=20
verbose 0 (138: primitive.py, options)
The allowed options for DensityPlot defined by a 25 x 25 data grid are:
cmap the name of a predefined colormap,
a list of colors or an instance of a
matplotlib Colormap. Type: import matplotlib.cm; matplotlib.cm.datad.keys()
for available colormap names.
interpolation What interpolation method to use
plot_points How many points to use for plotting precision
</code></pre>
<p>This is an inconvenience because it's not pretty to force it via the underlying matplotlib objects, but I might be missing something.</p>
http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?comment=20825#post-id-20825You should also be able to specify the zorder when you do the plot. So h1 = list_plot(zip(x_coords, y_coords), zorder=1), etc. We should probably make the default zorder for things like density plots be behind other plots that are more like lines or points.Thu, 24 Nov 2011 18:07:40 -0600http://ask.sagemath.org/question/8510/overlaying-plots-in-a-specific-order/?comment=20825#post-id-20825