【发布时间】:2018-04-28 21:09:08
【问题描述】:
我有一个包含四列的 csv 文件。时间的第一列,第二、第三和第四列是加速度计读数。我想在 X 轴上绘制时间,在 Y 轴上绘制加速度计读数。
样本数据:
0 1.0969 9.7721 0.614
20 1.1146 9.7501 0.7444
40 1.1146 9.7501 0.7444
60 1.0124 9.7151 0.7169
79 1.0124 9.7151 0.7169
100 1.0927 9.7324 0.7356
120 1.0927 9.7324 0.7356
这是我目前所拥有的。
from numpy import genfromtxt
import csv
import matplotlib.pyplot as plt
#import numpy as np
# Open the desired file for reading
f = open('walk-shoe.csv', "rb")
# create a object of csv class and read the file
# use ',' as a delimiter
reader = csv.reader(f, delimiter=',')
time_row = 0
accel_1_row = 0
accel_2_row = 0
accel_3_row = 0
time = []
accel_1 = []
accel_2 = []
accel_3 = []
# create a list of 'Time in ms'
for row in reader:
# Skip the first row
time_row = time_row + 1
if time_row == 1:
continue
time.append(row[0])
accel_1.append(row[1])
accel_2.append(row[2])
accel_3.append(row[3])
# print the contents of the list
# print time
#print accel_1
#print accel_2
#print accel_3
# append all the list accelerometer list together
final_accel = []
final_accel.append(accel_1)
final_accel.append(accel_2)
final_accel.append(accel_3)
#print final_accel
# plot the graph
for i in range(len(final_accel)):
plt.plot(time,[pt[i] for pt in final_accel],label = 'id %s'%i)
plt.legend()
plt.show()
我想在 y 轴上绘制所有传感器读数,在 x 轴上绘制时间
【问题讨论】:
-
ValueError x and y must have same first dimension, but have shapes是最常见的错误之一。你谷歌了吗?您正在尝试针对 1001 绘制 3 个值。这当然行不通。 -
感谢您的及时回复。我试过了,但没有得到任何合适的解决方案。
-
为什么不呢?任何解决方案基本上都会告诉您不要尝试将不同长度的列表绘制为 x 和 y 值。
-
我理解错误,但我不知道如何解决它。我需要在同一张图上绘制多个加速度计读数图。 final_accel 列表追加了多个列表。现在我想画出 len(time) = len(final_accel[1]) = len(fina_accel[2]) =len(fina_accel[3])。这个link 是我得到的最接近的,但对我不起作用。我不知道为什么
-
然后edit 的问题显示什么不起作用。另请阅读minimal reproducible example。我们没有您的输入数据,因此您需要使用一些虚拟数据创建一个示例。
标签: python python-2.7 csv matplotlib plot