[英]ValueError with scipy.signal.find_peaks
Right now I'm struggling with plotting the graph that should be showing the peaks of my dataset, but it looks like the find_peaks function is cutting off every data point that doesn't fit into the peak detection.现在我正在努力绘制应该显示我的数据集峰值的图表,但看起来 find_peaks function 正在切断不适合峰值检测的每个数据点。 Does anybody know how I can still plot the graphs by maybe replacing the data points that don't fit with zeros or is there any other possibility?
有谁知道我如何通过替换不适合零的数据点或者是否有任何其他可能性来仍然可以 plot 图表?
I am getting the following Error Message:我收到以下错误消息:
ValueError: x and y must have same first dimension, but have shapes (800,) and (105,)
ValueError:x 和 y 必须具有相同的第一维,但具有形状 (800,) 和 (105,)
def plot():
i = 1
d_time, d_x, d_y, d_z = [], [], [], []
columns = ["Time", "y", "x", "z"]
df = pd.read_csv("mydata.csv", usecols=columns)
for zeile in df.Time:
if i % 30 == 0:
d_time.append(df.Time[i])
d_x.append(df.x[i])
d_y.append(df.y[i])
d_z.append(df.z[i])
i += 1
elif i > 24000:
break
else:
i += 1
fig = plt.figure(dpi=64, figsize=(100, 60))
p_z, _ = scipy.signal.find_peaks(d_z, 0, distance=5)
plt.plot(d_time, d_z, c='red', label="Z-Achse")
plt.plot(d_time, p_z, "x", c='blue', label="Peaks Z-Achse")
plt.title("Peak Detection", fontsize=16)
plt.xlabel('t(s)', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("a(m/s²)", fontsize=16)
plt.tick_params(axis='both', which='major')
plt.legend()
plt.show()
plot()
Link to Mydata.csv: https://cdn.discordapp.com/attachments/635516210473336844/945630182415405106/mydata.csv链接到 Mydata.csv: https://cdn.discordapp.com/attachments/635516210473336844/945630182415405106/mydata.csv
Your problem lies in the fact (as you also mentioned) that p_z
cuts a lot of points so d_time
and p_z
don't have the same length.您的问题在于(正如您还提到的那样)
p_z
削减了很多点,因此d_time
和p_z
的长度不同。 Therefore, you get the error.因此,您会收到错误消息。 What you can do is create a
np.linspace
equal to the length of d_time
and plot it with the new time vector.你可以做的是创建一个
np.linspace
等于d_time
的长度和 plot 它与新的时间向量。 Following is my solution:以下是我的解决方案:
import matplotlib.pyplot as plt
import pandas as pd
from scipy import signal
import numpy as np
def plot():
i=1
d_time, d_x, d_y, d_z = [], [], [], []
columns = ["Time", "y", "x", "z"]
df = pd.read_csv("mydata.csv", usecols = columns)
for zeile in df.Time:
if i % 30 == 0:
d_time.append(df.Time[i])
d_x.append(df.x[i])
d_y.append(df.y[i])
d_z.append(df.z[i])
i+=1
elif i > 24000:
break
else:
i+=1
fig = plt.figure(dpi=64, figsize=(100, 60))
p_z, _ = signal.find_peaks(d_z, 0, distance=5)
new_time = np.linspace(d_time[0], d_time[-1], len(p_z))
plt.plot(d_time, d_z, c='red', label = "Z-Achse")
# plt.plot(d_time, p_z, "x", c='blue', label = "Peaks Z-Achse")
plt.plot(new_time, _['peak_heights'], "x", c='blue', label = "Peaks Z-Achse")
plt.title("Peak Detection", fontsize=16)
plt.xlabel('t(s)', fontsize=16)
plt.yscale("log")
fig.autofmt_xdate()
plt.ylabel("a(m/s²)", fontsize=16)
plt.tick_params(axis='both', which='major')
plt.legend()
plt.show()
plot()
As you can see in line 28, I have created a new time vector of length equal to d_time
which solves your problem.正如您在第 28 行中看到的,我创建了一个长度等于
d_time
的新时间向量,它解决了您的问题。 Also, I have changed the y-axis to log scale (line 35) for seeing the results better.此外,我已将 y 轴更改为对数刻度(第 35 行)以便更好地查看结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.