Black Lives Matter. Please consider donating to Black Girls Code today.

Plot ordering with scatter and contours

Hi,

I am trying to plot a contour plot of a probability density function and a scatter plot of the data points generated from this distribution. However, for large data sets, the contour plot is not exactly visible because the data points are plotted over it.

So, I was trying to figure out how to change the order of plotting. I noticed that if I have only scatter plots, then the ordering in the data list is influencing the order of plotting but that is not the case when I have the aforementioned contour plot. Scatter plots are always over the contour plot.

Here’s the code I’ve been writing:
import numpy as np

import plotly as py
import plotly.graph_objs as go

X = np.arange(-5, 5, 0.1)
Y = np.arange(0, 4, 0.1)
x, y = np.meshgrid(X, Y)
p = mlab.bivariate_normal(x, y, 1.5, 0.8, 1, 2, 0.6)
mean_pre = np.array([1,2])
cov_pre = np.array([[1.5**2, 0.6],[0.6, 0.8**2]])
sample = np.random.multivariate_normal(mean_pre, cov_pre, 10000).T

py.offline.init_notebook_mode(connected=True)
layout = go.Layout(
    title='True Distribution and Sampled Data Points',
    yaxis=dict(
        title='x'
    ),
    xaxis=dict(
        title='y'
    )
)
trace1 = go.Scattergl(
    x=sample[0], 
    y=sample[1],
    mode='markers',
    name='Sampled Data',
    marker = dict(
        size = 2,
        opacity = 0.6
    )
)

contours = go.Contour(
        z=p,
        x=X,
        y=Y,
        colorscale='Jet',
        contours=dict(
            coloring='lines',
        ),
        line = dict(
            width=2
        )
    )

fig = go.Figure(data=[trace1, contours], layout=layout)
py.offline.iplot(fig)`

Same behavior still exists 2 years later. It would be nice to have a “zorder” parameter.