Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

Why plotly function needs different data formats in Python and R to plot 3d surface plot?

Here is my Python code, which gives me a plot:

from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import cufflinks as cf
import pandas as pd
import numpy as np
%matplotlib inline
df3 = pd.DataFrame({'x':[1,2,3,4,5],'y':[10,20,30,20,10],'z':[5,4,3,2,1]})
df3.iplot(kind='surface',colorscale='rdylbu')

Pyhton output is: enter image description here Then I tried to recreate the same data frame and plot the plot, however, got an error


x<-c(1,2,3,4,5)
y=c(10,20,30,20,10)
z=c(5,4,3,2,1)

df3 = data.frame(x,y,z)
plot_ly(x = df3$x, y = df3$y, z = df3$z) %>% add_surface()
Error: `z` must be a numeric matrix

#also tried 
plot_ly(data = data.frame(df)) %>% add_surface()
Error: Must supply `z` attribute

How can I get the plot in R? Also assume I got z as categorical z=c("a","b","c","d","e"), can I plot with this values?

question from:https://stackoverflow.com/questions/65950835/why-plotly-function-needs-different-data-formats-in-python-and-r-to-plot-3d-surf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...