Ask Your Question
2

Overlaying plots in a specific order

asked 13 years ago

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

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 13 years ago

DSM gravatar image

updated 13 years ago

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.

Preview: (hide)
link

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 ( 13 years ago )

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 ( 13 years ago )

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

kcrisman gravatar imagekcrisman ( 13 years ago )

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 ( 13 years ago )

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: 13 years ago

Seen: 3,253 times

Last updated: Nov 24 '11