Python 通过从CSV中读取coulmns来绘制图形

Python 通过从CSV中读取coulmns来绘制图形,python,python-2.7,csv,matplotlib,plot,Python,Python 2.7,Csv,Matplotlib,Plot,我有一个包含四列的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

我有一个包含四列的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轴上

您似乎要在您给出的代码中导入numpy,因此我认为这意味着库对您可用。Numpy让您可以非常轻松地使用

然后,您可以创建一个for循环,该循环遍历第1列到第3列,并根据第0列(时间)绘制数据


值错误x和y必须具有相同的第一维度,但具有形状是最常见的错误之一。你用谷歌搜索了吗?您正试图根据1001绘制3个值。这当然不行。谢谢你的及时回复。我试过了,但没有得到任何合适的解决方案。为什么不呢?任何解决方案基本上都会告诉您不要尝试将不同长度的列表绘制为x和y值。我理解错误,但我不知道如何解决它。我需要在同一个图表上绘制多个加速度计读数图。最后的加速列表附加了多个列表。现在我想画出len(时间)=len(最终加速度[1])=len(最终加速度[2])=len(最终加速度[3])。这是我得到的最接近的,但对我不起作用。我不知道为什么要问这个问题来说明什么不起作用。也读。我们没有您的输入数据,因此您需要创建一个包含一些虚拟数据的示例。
import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('walk-shoe.csv', delimiter=',', dtype=float)
print (data)
#[[   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]]

for i in range(1,data.shape[1]):
    plt.plot(data[:,0], data[:,i], label='id %s' %i)

plt.legend()
plt.show()