Plotly 3d surface graph has incorrect x and y axis values

和自甴很熟 提交于 2019-12-12 09:04:48

问题


I would like to make a 3d surface plot with plotly. This is going quite well, however the values on the x and y axis make no sense. They are much higher than they should be. I used a matrix for the plot, the x and y values are the row and column names and the z value (called elevation) of course the matrix itself. Plotly doesn't seem to use the values of the x and y data. The x and y values range from 0 to 6 in the matrix. This is a small sample of my matrix:

head(m.dune)    
>       1.90 1.95 2    2.05 2.01 2.15 
> 0     NA   NA   NA   NA   NA   NA
> 0.05  NA   NA   NA   NA   NA   NA
> 0.10  1.14 1.14 NA   NA   NA   NA
> 0.15  1.15 1.15 1.15 1.15 1.16 1.16
> 0.20  1.16 1.16 1.16 1.16 1.16 1.17
> 0.25  1.17 1.17 1.17 1.17 1.18 1.18

This is the code I have been using:

dune.plot <- plot_ly(z = m.dune, type = "surface") %>%
layout(
  scene=list(
    xaxis=list(title='x (m)'),
    yaxis=list(title='y (m)'),
    zaxis=list(title='Elevation (m)')))
dune.plot

The graph looks like this, with incorrect x and y axis, I am not sure how I can get correct values.


回答1:


As Jimbou pointed out in his comment the values on the x and y axes are the number of rows and columns of your matrix. Since I have a cell size of 0.05, the values do not represent the correct distance. To show the correct distance you have to add the correct x and y values as list in the plotly graph. These x and y values have to have the same size, otherwise a part of the graphs gets cutoff. The code to get the correct x and y values is below:

y <- x <- as.numeric(rownames(m.dune))
# More rows than column, and the x and y should have the same size

dune1.plot <- plot_ly(z = m.dune1, x= x, y=y, type = "surface") %>%
layout(
  scene=list(
    xaxis=list(title='x (m)'),
    yaxis=list(title='y (m)'),
    zaxis=list(title='Elevation (m)')))
dune1.plot


来源:https://stackoverflow.com/questions/39576455/plotly-3d-surface-graph-has-incorrect-x-and-y-axis-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!