Mj111
Mj111

Reputation: 19

how to fit the equation of plane?

I am trying to fit the equation of plane on the 3d-surface plot as can be seen in Plot. I have also attached the data link. I don't have any idea of how to fit the functions on surface plots. It is a part of small project. Please do help me.

%matplotlib qt
#%matplotlib notebook
import numpy as np
import pandas as pd
#import seaborn as sns
import math 
import matplotlib.pyplot as plt 
import csv
from mpl_toolkits.mplot3d import axes3d
from matplotlib import interactive
interactive(True)
from matplotlib import colors as mcolors
from scipy.optimize import leastsq
from scipy.optimize 


df = pd.read_csv('output.csv', usecols=range(0, 31), header = None)
#df.loc[0:30, 0:31]
#df = df[0:len(Data)-1]
df_3 = df


x,y = np.meshgrid(df.columns.astype(float), df_1.index, sparse=True)
z = df_3.values

def quadratic(x, y, a, b, c, d): 
    return a*x + b*y + c*z + d

def res(params, zData):
    residuals = [
    p[2] - quadratic(p[0], p[1], params[0], params[1], params[2], params[3]) for z in zData]
    return numpy.linalg.norm(residuals)

result = scipy.optimize.minimize(residuals,(1, 1, 0, 0, 0, 0),#starting point
                                 args=z)

fig = plt.figure(figsize=(14,15))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(x, y, z, edgecolor='k', cmap='viridis', alpha = 0.99 ,linewidth=0, rstride=1, cstride=1, 
                       antialiased=False, shade=False)
fig.colorbar(surf,ax=ax, shrink=0.5, aspect=20)
#plt.show()

ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False

# Now set color to white (or whatever is "invisible")
ax.xaxis.pane.set_edgecolor('w')
ax.yaxis.pane.set_edgecolor('w')
ax.zaxis.pane.set_edgecolor('w')

plt.show()

Upvotes: 0

Views: 129

Answers (1)

Divyang Prajapati
Divyang Prajapati

Reputation: 158

May be this should work.

#!/usr/bin/env python

import pandas as pd
import numpy as np
from numpy.linalg import inv

#%% Function

def PlaneFit(x, y, z):

    Mat1 = np.array([ [x.sum(),          y.sum(), x.size],
                      [(x**2).sum(), (x*y).sum(), x.sum()],
                      [(z*x).sum(),  (z*y).sum(), z.sum()],
                     ])

    Mat2 = np.array([ [z.sum()],  [(x*z).sum()], [(z**2).sum()] ])

    Mat = np.dot( inv(Mat1), Mat2 )

    m, n, d = float(Mat[0]), float(Mat[1]), float(Mat[2])

    return m, n, d

#%% values of z

df = pd.read_csv('output.csv', usecols=range(0, 31), header = None)
df_1 = df[0:31]

z = np.array(df_1)

#%% x and y

x = np.linspace(0, 1, 31)
y = np.linspace(0, 1, 31)

x, y = np.meshgrid(x, y)

#%% call function and make plane

m, n, d = PlaneFit(x, y, z)

Zplane = m* x + n* y + d

#%% Plotting

from mpl_toolkits.mplot3d import axes3d;
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, edgecolor='k', cmap='viridis', alpha = 0.99 ,linewidth=0, rstride=1, cstride=1, 
                       antialiased=True, shade=False )
ax.plot_surface(x, y, Zplane, edgecolor='k', color='cyan' )
plt.show()

Upvotes: 1

Related Questions