0

I'm trying to create a piecewise linear interpolation routine and I'm pretty new to all of this so I'm very uncertain of what needs to be done.

I've generate a set of data points in 3D which gives variation in all 3 directions. I want to interpolate between these data points and plot in 3D.

The current data set is much smaller than the final one will be. Linear interpolation is important.

here's the current code

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy.interpolate as interp


x = np.linspace(-1.3,1.3,10)
y1 = np.linspace(.5,0.,5)
y2 = np.linspace(0.,.5,5)
y = np.hstack((y1,y2))
z1 = np.linspace(.1,0.,5)
z2 = np.linspace(0.,.1,5)
z = np.hstack((z1,z2))

data = np.dstack([x,y,z])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')


f = interp.interp2d(x, y, z, kind='linear')
xnew = np.linspace(-1.3,1.3,100)
y1new = np.linspace(.5,0.,50)
y2new = np.linspace(0.,.5,50)
ynew = np.hstack((y1new,y2new))
znew = f(xnew,ynew)

ax.plot(x,y,znew, 'b-')
ax.scatter(x,y,z,'ro')
plt.show()

As I said, dataset is just to add variation. The real set will be much bigger but have less variation. I don't really understand the interpolation tool and the scipy documentation isn't very clear

would appreciate suggestions

gravity121
  • 33
  • 7
  • Did you see the example here https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.interp2d.html – Joe Feb 22 '18 at 14:04
  • Plotting examples can be found here https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html – Joe Feb 22 '18 at 14:06
  • Hi Joe, thanks for your reply. Yes I've seen it thanks but it plots a 2D representation. I want a 3D representation and what I get is something completely weird and the lines don't go through the data points. So either I'm using the incorrect interpolation function or I'm messing up on the 3D plotting. The example isn't helpful for my situation unfortunately. – gravity121 Feb 22 '18 at 15:53
  • Try to adjust this function to interp2: https://stackoverflow.com/a/26536822/7919597 – Joe Feb 23 '18 at 07:40

1 Answers1

0

2D ok. Please help with 3D

What I'm trying to do is build something that takes data points for deflections of a beam an interpolates between the data points. I wanted to to this in 3D and get a 3D plot showing the deflection along the x-axis in both y and z directions at the same time. As a stop gap measure I've used the below code to individually show deflection in y dir and z dir. Note, the data set is randomly generated for the moment. Some choices might look strange at the mo, but that's to sorta stick to the kinda range the final data set will use. The code below works for a 2D system so may be helpful to someone. I'd still really appreciate if someone could help me do this in 3D.

import numpy as np
import matplotlib.pyplot as plt

from scipy.interpolate import CubicSpline

u=10


x = np.linspace(-1.3,1.3,u) #regular x-data 
y = np.random.random_sample(u)/4 #random y data 
z = np.random.random_sample(u)/10 # random zdata 
ynone = np.ones(u)*0.1 #no deflection dataset
znone = np.ones(u)*0.05

xspace = np.linspace(-1.3, 1.3, u*100)


ydefl = CubicSpline(x, y) #creating cubinc spline function for original data
zdefl = CubicSpline(x, z)


plt.subplot(2, 1, 1)
plt.plot(x, ynone, '-',label='y - no deflection')
plt.plot(x, y, 'go',label='y-deflection data')
plt.plot(xspace, ydefl(xspace), label='spline') #plot xspace vs spline         function of xspace
plt.title('X [m]s')
plt.ylabel('Y [m]')
plt.legend(loc='best', ncol=3)

plt.subplot(2, 1, 2)
plt.plot(x, znone, '-',label='z - no deflection')
plt.plot(x, z, 'go',label='z-deflection data')
plt.plot(xspace, zdefl(xspace),label='spline')
plt.xlabel('X [m]')
plt.ylabel('Z [m]')
plt.legend(loc='best', ncol=3)

plt.show()

enter image description here

gravity121
  • 33
  • 7