简体   繁体   中英

How can I cut a piece away from a plot and set the point I need to zero?

In my work I have the task to read in a CSV file and do calculations with it. The CSV file consists of 9 different columns and about 150 lines with different values acquired from sensors. First the horizontal acceleration was determined, from which the distance was derived by double integration. This represents the lower plot of the two plots in the picture. The upper plot represents the so-called force data. The orange graph shows the plot over the 9th column of the CSV file and the blue graph shows the plot over the 7th column of the CSV file.

情节

As you can see I have drawn two vertical lines in the lower plot in the picture. These lines represent the x-value, which in the upper plot is the global minimum of the orange function and the intersection with the blue function. Now I want to do the following, but I need some help: While I want the intersection point between the first vertical line and the graph to be (0,0), ie the function has to be moved down. How do I achieve this? Furthermore, the piece of the function before this first intersection point (shown in purple) should be omitted, so that the function really only starts at this point. How can I do this? In the following picture I try to demonstrate how I would like to do that: 在此处输入图像描述

If you need my code, here you can see it:

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)

I hope it's clear what I want to do. Thanks for helping me!

I didn't dig all the way through your code, but the following tricks may be useful.

Say you have x and y values:

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

Now, you only want the values corresponding to, say, .5 < x < 1.5 . First, create a boolean mask for the arrays as follows:

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

(If this seems magical, then run x < 1.5 in your interpreter and observe the results). Then use this mask to select your desired x and y values:

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

Then, you can translate all these values so that the first x,y pair is at the origin:

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

Is this the type of thing you were looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM