Ask Your Question
2

Overlaying plots in a specific order

asked 2011-11-24 18:51:40 +0100

Jason gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2011-11-24 19:33:24 +0100

DSM gravatar image

updated 2011-11-24 19:54:07 +0100

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

image description

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.

edit flag offensive delete link more

Comments

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[0] are different types, that seems a little unintuitive.

Jason gravatar imageJason ( 2011-11-24 20:57:24 +0100 )edit

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.

Jason Grout gravatar imageJason Grout ( 2011-11-25 01:07:40 +0100 )edit

@Jason Grout: Good idea - if you open a ticket, cc: me.

kcrisman gravatar imagekcrisman ( 2011-11-25 21:12:57 +0100 )edit

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.

kcrisman gravatar imagekcrisman ( 2011-11-25 21:13:52 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-11-24 18:51:40 +0100

Seen: 3,120 times

Last updated: Nov 24 '11