Black Lives Matter. Please consider donating to Black Girls Code today.

How to create 3D surface plot wih three vectors?

I am a newbie to plotly and I have a basic question of how to do a 3D surface plot. I have raw data in the form of 3 vectors X [ ], Y [ ], Z [ ]. I want to plot Z surface as function of X and Y. I am using python. I looked into the tutorial (https://plot.ly/python/3d-surface-plots/) but of no use since the input data structure is different to my case. Should I need to change the data structure or is there a way to plot it directly from vectors without making any changes. Thanks in advance for your time and effort.

In order to plot a surface from your data,
the list Z should have len(Z)=len(X)*len(Y). If this is the case then you have to convert it to a reshaped numpy array:

z=np.array(Z).reshape((len(Y), len(X))

and define the Plotly surface as follows:

surf=Surface(x=X,
             y=Y,
             z=z, 
             colorscale=your_colorscale
            )

Thanks empet. len(Z) is = len(X)*len(Y). I am a newbie to both python and plotly. I couldn`t get it plotted. Can you please help me with next couple of lines/syntax of code? Thanks.

z = np.array(Z).reshape((len(Y), len(X))) data = go.Surface(x=X,y=Y,z=z) fig = go.Figure(data=data)

If I execute the above, it throws out some error.
PlotlyDictValueError: 'data' has invalid value inside 'figure'

What am I doing wrong in here?

If I do this,

X = [1,2,3,4] Y = [1,2,3] Z = [] for i in X: for j in Y: Z.append(i*j) print (len(X)) print (len(Y)) print (len(Z)) z = np.array(Z).reshape((len(Y), len(X))) surf = go.Surface(z) data = [surf] fig = go.Figure(data=data)

It throws out
ValueError: dictionary update sequence element #0 has length 4; 2 is required

Got it working.

surf = go.Surface(z=z) data = [surf] py.iplot(data)

Thanks.