简体   繁体   中英

Overlaying line on contour plot using Plotly

I'd like to overlay a line on top a Plotly contour plot, similar to overlaying a line on an image of a matrix in which the intensity represents position in z within R3 :

# Generate an arbitrary matrix
m <- matrix(sin(1:6^2) * 1:6, nrow = 6)

# Define a path
path <- data.frame(x = c(0:7), y = c(0, 1, 2, 2, 3, 3, 4, 6))

image(x = 1:6, y = 1:6, z = m, col = gray.colors(20), xlab = "x", ylab = "y")
lines(path$x, path$y)

Which renders:

线和矩阵图

Using Plotly, I have attempted

library(plotly)
plot_ly(x = 1:6, y = 1:6, z = t(m), type = "contour") %>% 
  add_lines(x = path$x, y = path$y)

Which generates a contour plot overlaid with a wireframe of empty R3 space rather than a line:

在此输入图像描述

You could try this:

plot_ly(x = 1:6, y = 1:6, z = t(m), type = "contour") %>% 
  add_trace(x = c(1, 2, 3, 4, 5, 6), y = c(1, 2, 3, 3, 4, 5), type = "scatter", mode = "line")

It gives you almost what you want. Hope it helps!

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM