Given a Dataframe with recorded acceleration values on 3-axis that are used to determine position vectors. After applying BSpline, there is a smooth curve which looks quite well.
As a next step, I want to show also the extent of acceleration using different colors. For that, Matplotlib line color in 3D gives a good base, although the result is somehow messy, and I cannot identify the root cause:
Here is the record of acceleration values in CSV:
,Counter,Acc.X,Acc.Y,Acc.Z
18,18,9.71,0.51,4.41
19,19,9.69,0.65,4.34
20,20,9.67,0.6,4.3
21,21,9.6,0.59,4.33
22,22,9.34,0.11,4.63
23,23,6.01,-2.75,8.43
24,24,0.58,-5.01,6.18
25,25,2.16,-2.19,0.8
26,26,7.42,-1.84,-4.85
27,27,12.56,-5.04,-8.95
28,28,14.78,-9.62,-8.07
29,29,10.82,-7.58,-5.8
30,30,11.05,-8.45,-3.82
And the code:
import pandas as pd
import numpy as np
from scipy.interpolate import splrep, BSpline
from mpl_toolkits import mplot3d
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from mpl_toolkits.mplot3d.art3d import Line3DCollection
from enum import Enum
class axis(Enum):
X = 1
Y = 2
Z = 3
records = pd.read_csv(filepath_or_buffer='input.csv', index_col=0, sep=',')
i = range(0, len(records.index)-1)
x = (records.iloc[1:].values-records.iloc[:-1]).loc[:,'Acc.X'].to_numpy()
y = (records.iloc[1:].values-records.iloc[:-1]).loc[:,'Acc.Y'].to_numpy()
z = (records.iloc[1:].values-records.iloc[:-1]).loc[:,'Acc.Z'].to_numpy()
ii = np.linspace(i.start, i.stop);
tckx0 = splrep(i, x, s=0)
tckx = splrep(i, x, s=50)
tcky = splrep(i, y, s=50)
tckz = splrep(i, z, s=50)
ax = plt.figure(figsize=(50, 50)).add_subplot(projection='3d')
ax.plot(BSpline(*tckx)(ii),BSpline(*tcky)(ii),BSpline(*tckz)(ii), 'o--', color='green')
plt.show()
It results the following figure:
Applying colors, firstly just a simple style such as 'magma':
# Coloring
points = np.array([BSpline(*tckx)(ii),BSpline(*tcky)(ii),BSpline(*tckz)(ii)]).transpose().reshape(-1,1,3)
segs = np.concatenate([points[:-1],points[1:]],axis=1)
lc = Line3DCollection(segs, cmap=plt.get_cmap('magma'))
lc.set_array(ii)
fig = plt.figure(figsize=(50, 50))
ax = fig.add_subplot(111, projection='3d')
ax.add_collection3d(lc)
plt.show()
The figure is completely messed:
I cannot identify the root cause, could you please help me?