Transform the intersect line of 2 surface plot to a line on contour plot

Dear Community,

I’d be content if I can find a solution for either of my questions about contour line.

Imagine I have 2 surface plots intersect each other. Now I want to make contour plot from ONE of the surface plot – I was wondering if there is a way to highlight the intersect line from the first double surface plot.

Thank you so much!

Sincerely,
JS

You could manually highlight the intersect line on the contour trace by creating a new scatter trace on top. To do this you will need to be able to describe the line of intersection as a series of points in Cartesian coordinates (e.g. (x1, y1), (x2, y2), ..., (xn, yn), (x1, y1)). Then you can create the scatter like below:

library(plotly)
p <- plot_ly(
  x = c(-9, -6, -5, -3, -1), 
  y = c(0, 1, 4, 5, 7), 
  z = matrix(c(10, 10.625, 12.5, 15.625, 20, 5.625, 6.25, 8.125, 11.25, 15.625, 2.5, 3.125, 5, 8.125, 12.5, 0.625, 1.25, 3.125,
        6.25, 10.625, 0, 0.625, 2.5, 5.625, 10), nrow = 5, ncol = 5), 
  type = "contour" 
) %>% add_trace(x = c(-8, -8, -3, -3, -8), y = c(1, 6, 6, 1, 1), type = 'scatter', mode='lines')

p

Thank you Michael! This is super close to what I imagined. However, I want to trace the contour line based on a given z value. I tried to play around with replace the x, y scatter with z, but it didn’t work as I expected. I’m afraid I’m just not quite familiar with the command. Any advice? Thanks a bunch!