# 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)?

edit retag close merge delete

Sort by » oldest newest most voted You might be interested in the answers to this question about zordering..

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.set_zorder(10)
sage: show(h) 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.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.

more

Thanks, that works. Why can't I do something like this: h1.set_zorder(10)+h2.set_zorder(11) ? I notice that h1 and h are different types, that seems a little unintuitive.

You 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.

As 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.