我正在尝试做一个程序,能够转换一个不同长度的csv到一个数据帧。然后,我尝试绘制数据帧的特定列,其中x值是索引除以1000 (采样率为1000 the )。
然而,我有一个奇怪的交互,它以线性的方式绘制列的值,并将点标记为一个值。它可能是索引值的图形,但我不确定,因为输出是一条曲线。我正在处理的当前数据集是大约12个设备的5000多个数据点(所有这些数据点都是同时采样的)。我将展示下面的数据片段。
下面是输出:

右图是左图的值的fft。
下面是输出应该是什么样子:

右边的图对于这两个是相同的,这是正确的输出。这告诉我,我的左图发生了一些奇怪的事情,但整个代码都是正确的。
下面是我的代码:
from scipy import fftpack
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import csv
temp = []
samplerate = 1000
with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR02.csv', 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
for row in csvreader:
if csvreader.line_num == 3:
temp.append(row)
if csvreader.line_num >= 6:
if row:
temp.append(row)
else:
break
df = pd.DataFrame(temp) #turns the array into a dataframe
df.columns = df.iloc[0] #sets the column names as the first row
df = df.drop(0) #drops the first row since it is now a duplicate of the column names
emg1 = df['Noraxon Desk Receiver - EMG1']
horiz = np.arange(0,len(emg1)/samplerate,1/samplerate) #getting the time domain in seconds
emgfft = fftpack.fft(emg1, horiz.size) #fft of the emg
emgfftabs = np.abs(emgfft) #absolute value of the fft values
xf = fftpack.fftfreq(horiz.size, (len(emg1) / samplerate) / samplerate) #frequency range
plt.figure()
plt.subplot(1,2,1)
plt.plot(horiz, emg1) #tried this one and the one below with the same result, both gave the wrong curved graph from the first picture
plt.plot(df['Noraxon Desk Receiver - EMG1']) #I didn't use both at the same time
ticks = plt.yticks(df['Noraxon Desk Receiver - EMG1'].values[::100]) #this is just here to make it readable, otherwise the y labels are a solid black bar
plt.subplot(1,2,2)
plt.plot(xf[0:len(xf)//2],2*emgfftabs[0:len(emgfftabs)//2])
plt.show()请注意,在我将其重命名为csv文件中的'emg1‘之前,'Noraxon Desk Receiver - EMG1’是原始的列名。
下面是数据集的示例

emg1的值可能对我们有所帮助,它是

指数似乎与我所能说的值是分不开的。emg1的数据类型最终为'object‘。我尝试使用'.tolist()',但也不起作用。
任何帮助解决这个问题的人都将不胜感激!
发布于 2018-12-05 11:11:18
使用df = pd.read_csv('your/csv/location.csv', index_col='your_index')应该会简化事情。
因此,如果您想对csv中的内容执行此操作,请将其作为'index':
df['index'] = df['index'] / 1000然后:
df.plot.bar(x='index', stacked=True)应该同时绘制emg1和emg2、em3等。如果您只想绘制emg1和emg2,则必须指定它。
在我看来,您只绘制了emg1 (正)值,所以matplotlib没有给出反射声波的形状。
发布于 2018-12-05 12:21:40
我想出了答案。显然,csvreader实际上将科学记数法读取为字符串。我只是添加了这行
emg1 = emg1.astype(np.float)而且它工作得很完美。
https://stackoverflow.com/questions/53624034
复制相似问题