matplotlib 如何根据权重对3D B样条进行着色

zzzyeukh  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(115)

给定具有用于确定位置向量的3轴上的记录的加速度值的 Dataframe 。应用B样条后,有一个平滑的曲线,看起来很好。
下一步,我想用不同的颜色来显示加速度的程度。为此,Matplotlib line color in 3D提供了一个很好的基础,尽管结果有点混乱,我无法确定根本原因:
以下是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

代码:

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()

结果如下图:

应用颜色,首先只是一个简单的风格,如'岩浆':

# 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()

数字完全乱了套:

我无法确定根本原因,您能帮助我吗?

j8yoct9x

j8yoct9x1#

我觉得你插错东西了。
要插值3D曲线,必须使用scipy的splprepsee this question)。
要插值1D曲线,必须使用scipy的splrep
在下面的代码中,我还绘制了初始x, y, z数据,以便您可以将结果与我的结果进行比较。

from scipy.interpolate import splrep, BSpline, splev, splprep
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from mpl_toolkits.mplot3d.art3d import Line3DCollection
import numpy as np
from enum import Enum
class axis(Enum):
    X = 1
    Y = 2
    Z = 3

from io import StringIO
import pandas as pd

mystr = StringIO("""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
""")
records = pd.read_csv(mystr, 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)

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot(x, y, z, 'ko', label="true data")
ax.plot(BSpline(*tckx)(ii),BSpline(*tcky)(ii),BSpline(*tckz)(ii),
        color='green', label="your interpolation")

# spline interpolation of a 3D curve
tck, u = splprep([x, y, z], s=2)
ii = np.linspace(0, 1)
xs, ys, zs = splev(ii, tck)

# spline interpolation of a 1D curve
a_x = records["Acc.X"].to_numpy()[:-1]
a_y = records["Acc.Y"].to_numpy()[:-1]
a_z = records["Acc.Z"].to_numpy()[:-1]
# color by magnitude of acceleration
a = np.sqrt(a_x**2 + a_y**2 + a_z**2)
tcka = splrep(np.linspace(0, 1, len(a)), a, s=0)
parameter = splev(ii, tcka)

ax.plot(xs, ys, zs, 'r.', label="new interpolation")

def get_segments(x, y, z):
    points = np.ma.array([x, y, z]).T.reshape(-1, 1, 3)
    return np.ma.concatenate([points[:-1], points[1:]], axis=1)

line = Line3DCollection(get_segments(xs, ys, zs), cmap="magma", array=parameter)
ax.add_collection(line)
fig.colorbar(line, label="acceleration")

ax.legend()
plt.show()

相关问题