7

I really can't find an answer on the forum. I want to create a 3d surface with plotly offline. I use sample points. The scene displays empty without a surface. Please help, thanks.

import pandas as pd
import numpy as np
from plotly.offline import download_plotlyjs, init_notebook_mode, plot
from plotly.graph_objs import *
init_notebook_mode()
df3=pd.DataFrame({'x':[1,2,3,4,5], 'y':[10,20,30,20,10], 'z':[5,4,3,2,1]})

trace0= Surface(x=df3['x'], y=df3['y'], z=df3['z'])
data=[trace0]
fig=dict(data=data)
plot(fig)
byouness
  • 1,746
  • 2
  • 24
  • 41
Pietruszka10
  • 71
  • 1
  • 3

1 Answers1

8

The problem is you haven't defined a surface of data, since your z data is just one-dimensional. For example, this should show a surface:

import pandas as pd
import numpy as np
from plotly.offline import download_plotlyjs, init_notebook_mode, plot
from plotly.graph_objs import *
init_notebook_mode()

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

trace0= Surface(x=df3['x'], y=df3['y'], z=df3['z'])
data=[trace0]
fig=dict(data=data)
plot(fig)
Adam
  • 414
  • 6
  • 13
  • I'm not sure I follow this... if I am looking at a 2D Gaussian distribution in particular.... shouldn't I have a single z for each x,y? – AyeTown Sep 16 '20 at 23:24
  • Yes. The above `z` is like a 2D matrix. Note that in Python if `a = [[1, 2]]`, then `a * 2` will be `[[1, 2], [1, 2]]`. So this was just a trivial example to demonstrate the form of the required data. – Adam Sep 17 '20 at 20:22