How to add a contour overlay to a 3D plotly in R?

I’m not familiar with R but it seems that you are adding two mesh3d traces to the figure.

What you will have to do is adding scatter plots.

Here a Python example:

import plotly.graph_objs as go
import numpy as np
import pandas as pd

# generate data
xx = np.arange(0, 2*np.pi, 0.4)
yy = np.arange(0, 5, 1)

# generate mesh
x, y = np.meshgrid(xx, yy)

# flatten x and y, create z coordinate
x=x.flatten()
y=y.flatten()
z = np.sin(x)

# create figure (mesh3d trace)
fig = go.Figure(
    data=go.Mesh3d(
        x=x,
        y=y,
        z=z,
        color='black',
        opacity=0.7
    ),
    layout={'width': 800, 'height':800}
)

# create scatter3d traces for the lines
for i in range(-1, 4):
    fig.add_trace(
        go.Scatter3d(
            x=xx, 
            y=np.ones_like(xx)*(i+1), 
            z=z, 
            mode='lines',
            line_width=5,
            line_color='white'
        )
    )
    
# create scatter3d traces for the lines
for i in range(0, 7):
    fig.add_trace(
        go.Scatter3d(
            x=np.ones_like(yy)*i, 
            y=yy, 
            z=np.ones(5)*np.sin(i), 
            mode='lines',
            line_width=5,
            line_color='red'
        )
    )
    
fig.show()


mrep 3D

2 Likes