首页 > 解决方案 > 如何从绘图中切出一块并将我需要的点设置为零?

问题描述

在我的工作中,我的任务是读取 CSV 文件并使用它进行计算。CSV 文件由 9 个不同的列和大约 150 行具有从传感器获取的不同值组成。首先确定水平加速度,通过双重积分从中得出距离。这代表了图片中两个地块的下部地块。上图代表所谓的力数据。橙色图显示 CSV 文件第 9 列的图,蓝色图显示 CSV 文件第 7 列的图。

情节

如您所见,我在图片的下部绘制了两条垂直线。这些线代表 x 值,在上图中是橙色函数的全局最小值和与蓝色函数的交点。现在我想做以下事情,但我需要一些帮助: 虽然我希望第一条垂直线和图形之间的交点为 (0,0),即函数必须向下移动。我如何实现这一目标?此外,在第一个交点之前的函数部分(以紫色显示)应该被省略,这样函数实际上只从这个点开始。我怎样才能做到这一点?在下面的图片中,我尝试展示我想如何做到这一点: 在此处输入图像描述

如果你需要我的代码,你可以在这里看到:

import numpy as np
import matplotlib.pyplot as plt
import math as m
import loaddataa as ld
import scipy.integrate as inte
from scipy.signal import find_peaks
import pandas as pd
import os

# Loading of the values

print(os.path.realpath(__file__))
a,b = os.path.split(os.path.realpath(__file__))

print(os.chdir(a))
print(os.chdir('..'))
print(os.chdir('..'))
path=os.getcwd()
path=path+"\\Data\\1 Fabienne\\Test1\\left foot\\50cm"
print(path)
dataListStride = ld.loadData(path)
indexStrideData = 0 
strideData = dataListStride[indexStrideData]

#%%Calculation of the horizontal acceleration

def horizontal(yAngle, yAcceleration, xAcceleration):
     a = ((m.cos(m.radians(yAngle)))*yAcceleration)-((m.sin(m.radians(yAngle)))*xAcceleration)
     return a
 
resultsHorizontal = list()

for i in range (len(strideData)):
    strideData_yAngle = strideData.to_numpy()[i, 2]
    strideData_xAcceleration = strideData.to_numpy()[i, 4]
    strideData_yAcceleration = strideData.to_numpy()[i, 5]
    resultsHorizontal.append(horizontal(strideData_yAngle, strideData_yAcceleration, strideData_xAcceleration))

resultsHorizontal.insert(0, 0)

#plt.plot(x_values, resultsHorizontal)

#%%
#x-axis "convert" into time: 100 Hertz makes 0.01 seconds
scale_factor = 0.01 
x_values = np.arange(len(resultsHorizontal)) * scale_factor

#Calculation of the global high and low points
heel_one=pd.Series(strideData.iloc[:,7])
plt.scatter(heel_one.idxmax()*scale_factor,heel_one.max(), color='red')
plt.scatter(heel_one.idxmin()*scale_factor,heel_one.min(), color='blue')

heel_two=pd.Series(strideData.iloc[:,9])
plt.scatter(heel_two.idxmax()*scale_factor,heel_two.max(), color='orange')
plt.scatter(heel_two.idxmin()*scale_factor,heel_two.min(), color='green')#!

#Plot of force data
plt.plot(x_values[:-1],strideData.iloc[:,7]) #force heel 
plt.plot(x_values[:-1],strideData.iloc[:,9]) #force toe

# while - loop to calculate the point of intersection with the blue function 
i = heel_one.idxmax()
while strideData.iloc[i,7] > strideData.iloc[i,9]:
    i = i-1

# Length calculation between global minimum orange function and intersection with blue function
laenge=(i-heel_two.idxmin())*scale_factor
print(laenge)

#%% Integration of horizontal acceleration
velocity = inte.cumtrapz(resultsHorizontal,x_values)
plt.plot(x_values[:-1], velocity)

#%% Integration of the velocity
s = inte.cumtrapz(velocity, x_values[:-1])
plt.plot(x_values[:-2],s)

我希望很清楚我想要做什么。谢谢你帮助我!

标签: pythonarraysnumpyplotcalculation

解决方案


我没有深入研究您的代码,但以下技巧可能有用。

说你有xy价值观:

x = np.linspace(0,3,100)
y = x**2

现在,您只需要对应于的值,例如.5 < x < 1.5. 首先,为数组创建一个布尔掩码,如下所示:

mask = np.logical_and(.5 < x, x < 1.5)

(如果这看起来很神奇,那么x < 1.5在你的解释器中运行并观察结果)。然后使用此掩码选择您想要xy值:

x_masked = x[mask]
y_masked = y[mask]

然后,您可以翻译所有这些值,以便第一x,y对位于原点:

x_translated = x_masked - x_masked[0]
y_translated = y_masked - y_masked[0]

这是你要找的东西吗?


推荐阅读