Creating 3D surface plots using three 1D arrays of floats in Plotly

Viewed 939

I am trying to create a 3D surface plot using float arrays. I tried the available answer but was not successful.

My code:

     ydata       xdata      zdata
0  667.029710  38.666953  2156.370026
1  673.518030  38.578639  2167.733673
2  928.285970  39.558060  2984.315454
3  947.973966  39.329530  3065.414025
4  904.826638  40.241960  3071.355114
5  901.673720  40.259228  2947.752668
6  910.980404  41.478425  2906.467214
7  916.008852  41.813555  2933.900440
8  913.971114  41.583315  2955.543549
9  933.040692  42.577816  2979.264288 

import plotly.graph_objects as go
import plotly.graph_objs
import pandas as pd
import numpy as np
xdata = df['xdata'].values
ydata = df['ydata'].values
zdata = df['zdata'].values
plotly.offline.plot({"data":[go.Surface(z=zdata, x=xdata, y=ydata)],
"layout":plotly.graph_objs.Layout(title='Some data', autosize=False,
                  width=500, height=500,
                  margin=dict(l=65, r=50, b=65, t=90))})

It is not producing the output. I see the 3d plot but not any data.

1 Answers

If the data is specified in one dimension, it won't be displayed because it won't be configured with 3D information. If you specify the entire data frame, as in the code, it will be displayed. Official Reference

import pandas as pd
import numpy as np
import io

data = '''
 ydata xdata zdata
0 667.029710 38.666953 2156.370026
1 673.518030 38.578639 2167.733673
2 928.285970 39.558060 2984.315454
3 947.973966 39.329530 3065.414025
4 904.826638 40.241960 3071.355114
5 901.673720 40.259228 2947.752668
6 910.980404 41.478425 2906.467214
7 916.008852 41.813555 2933.900440
8 913.971114 41.583315 2955.543549
9 933.040692 42.577816 2979.264288 
'''

df = pd.read_csv(io.StringIO(data), sep='\s+')

import plotly.graph_objects as go
import plotly.graph_objs

# xdata = df['xdata'].values
# ydata = df['ydata'].values
# zdata = df['zdata'].values

plotly.offline.plot({"data":[go.Surface(z=df.values)],
"layout":plotly.graph_objs.Layout(title='Some data', autosize=False,
                  width=500, height=500,
                  margin=dict(l=65, r=50, b=65, t=90))})

enter image description here

Related