Search code examples
pythonplotlysurface

3D Surface plot is empty


I am following an online course on data science which uses Plot.ly with Cufflinks to plot data. I am trying to understand how the surface plot works. This is the data sample provided by the course author:

df3 = pd.DataFrame({'x':[1,2,3,4,5],'y':[10,20,30,20,10],'z':[5,4,3,2,1]})

I tried to plot it like this, but the plot comes up empty:

df3.iplot(kind='surface',colorscale='rdylbu', x='x', y='y', z='z')

In the figure display, when I click "Export to plot.ly", the generated spreadsheet matches my dataset, i.e., x = [1, 2, 3, 4, 5], y = [10, 20, 30, 20, 10], and z = [5, 4, 3, 2, 1]. But the figure is still empty. Based on the dataset, I expected the surface plot to contain the following points (in x, y, z order):

  • (1, 10, 5)
  • (2, 20, 4)
  • (3, 30, 3)
  • (4, 20, 2)
  • (5, 10, 1)

Solution

  • I am not sure how the author of the online course produced a surface plot using the data from df3 because z needs to be a 2D array (the documentation can be viewed here), and df3.iplot(kind='surface',colorscale='rdylbu', x='x', y='y', z='z') won't be understood by plotly because this passes a 1D array to the parameter z. Perhaps they were using an earlier version of plotly?

    As far as I can tell, this question is not well defined because there are multiple surface plots that could include those 5 points, but for the purpose of obtaining a surface plot, I will make up a possible 2D array for the z column of df3. Note that you could change any of the values in the array being passed to z and plotly would be able to render it as long as the shape is consistent with x and y.

    df3 = pd.DataFrame({'x':[1,2,3,4,5],'y':[10,20,30,20,10],'z':[[5,4,0,0,0],[3,4,0,0,0],[2,2,3,0,0],[1,1,1,2,0],[0,0,0,0,1]]})
    df3.iplot(kind='surface',colorscale='rdylbu', x='x', y='y', z='z')
    

    enter image description here