2
votes

I want to understand how to correctly do contour plots with plotly. In the code below I have x,y,z so I interpolate to have a more defined range with the interp in akima package. I plot the result first with plotly then with filled.contour. The result of the plotly is wrong but I like more its aesthetics in the filled contour the results is correct.

What am I doing wrong with plotly?

require(akima)
require(plotly)

x = rand(15,1)
y = rand(15,1)
z = rand(15,1)

a = interp(x, y, z)

p = plot_ly(x = a$x,
            y = a$y,
            z = a$z,
            type = "contour")
p

filled.contour(a$x,a$y,a$z)
1

1 Answers

2
votes

Plotly expects a little bit different matrix arrangement. Here is a fix:

require(akima)
require(plotly)
library(pracma)
set.seed(1)
x = rand(15,1)
y = rand(15,1)
z = rand(15,1)

a = interp(x, y, z)

plot_ly(x = a$x,
        y = a$y,
        z = matrix(a$z, nrow = length(a$y), byrow = TRUE),
        type = "contour")

enter image description here

filled.contour(a$x,a$y,a$z)

enter image description here

without the matrix rearrangement:

plot_ly(x = a$x,
        y = a$y,
        z = a$z,
        type = "contour")

enter image description here