Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Fit Surface Plot through Scatter Points 3D

I Just need a good way to generate Z 2D array to fit a surface through the scatter plots generated with the help of X,Y,Z axis.
All i Have is the following:

x : [3, 4, 6, 5, 5, 4, 6, 5, 1, 3, 2, 0, 1, 0, 2, 4, 5, 0, 2, 3, 5, 5, 0, 1, 4, 0, 2, 3, 4, 6, 4, 0, 4, 5, 6, 6, 4, 4, 3, 4, 6, 2, 1, 1, 5, 2, 6, 0, 2, 2]
y : [3, 1, 6, 2, 5, 2, 2, 4, 6, 3, 1, 4, 3, 0, 1, 2, 4, 3, 2, 5, 0, 1, 0, 4, 1, 1, 4, 3, 2, 1, 0, 3, 6, 5, 1, 5, 4, 4, 0, 6, 3, 6, 6, 3, 2, 3, 2, 5, 0, 1]
z : [5, 1, 4, 5, 3, 1, 5, 6, 0, 1, 1, 3, 3, 2, 4, 3, 5, 2, 0, 1, 6, 0, 2, 6, 0, 1, 6, 2, 2, 0, 6, 5, 2, 2, 0, 5, 6, 6, 5, 4, 6, 1, 2, 2, 4, 3, 3, 0, 0, 0]

I have used the following code to generate the Z 2D array. But the plot doesnt pass through most of the points

x=np.asarray([3, 4, 6, 5, 5, 4, 6, 5, 1, 3, 2, 0, 1, 0, 2, 4, 5, 0, 2, 3, 5, 5, 0, 1, 4, 0, 2, 3, 4, 6, 4, 0, 4, 5, 6, 6, 4, 4, 3, 4, 6, 2, 1, 1, 5, 2, 6, 0, 2, 2]);
y=np.asarray([3, 1, 6, 2, 5, 2, 2, 4, 6, 3, 1, 4, 3, 0, 1, 2, 4, 3, 2, 5, 0, 1, 0, 4, 1, 1, 4, 3, 2, 1, 0, 3, 6, 5, 1, 5, 4, 4, 0, 6, 3, 6, 6, 3, 2, 3, 2, 5, 0, 1]);
z=np.asarray([5, 1, 4, 5, 3, 1, 5, 6, 0, 1, 1, 3, 3, 2, 4, 3, 5, 2, 0, 1, 6, 0, 2, 6, 0, 1, 6, 2, 2, 0, 6, 5, 2, 2, 0, 5, 6, 6, 5, 4, 6, 1, 2, 2, 4, 3, 3, 0, 0, 0]);

xi=np.linspace(min(x), max(x),10)
yi=np.linspace(min(y),max(y),10)
zi = griddata((x,y), z, (xi, yi), method='linear')

X,Y= np.meshgrid(xi,yi)
Z = np.nan_to_num(griddata((x,y), z, (X, Y), method='cubic'))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

plt.savefig('surface.pdf')

print type(Z)

print "["
for row in Z:
    print str(list(row)) + ","
print "]" 

Please help me with this one. Thanks!

@etienne can you provide any insights?

If I understand your question correctly. it isn’t really related to plotly, plotly.js or any of out API libraries.

I think you’re only looking for info on how to perform a 2D fit in python. This here looks like a solid place to start.