简体   繁体   English

用python绘制CSV数据

[英]Plotting CSV data with python

I have a naive question about plotting csv data with python. 我对用python绘制csv数据有一个幼稚的问题。

I have a csv file looks like: 我有一个csv文件,看起来像:

SubjID SubjGrp GoRT SSRT  StopPoss
1   control 568.56  320.22  0.58
2   control 680.08  314.84  0.54

I want to plotting those data like: 我想绘制这些数据,例如: 在此处输入图片说明

I tried following syntax: 我尝试了以下语法:

import numpy as np
import matplotlib.pyplot as plt

d1=csv2rec('stop-allrun-2.csv',delimiter='\t', names=['SubjID', 'GoRT','SSRT','StopPoss'])

x=d1['SubjID']

y=['GoRT']

plot(x,y)

then popup the error message: 然后弹出错误信息:

ValueError: x and y must have same first dimension ValueError:x和y必须具有相同的第一维

Is there anything I can do to improve this script? 我有什么办法可以改善这个脚本?

Use the space as a delimiter ( ' ' ), then get all data except the column name and convert it from string to integer or float. 使用空格作为定界符( ' ' ),然后获取除列名之外的所有数据,并将其从字符串转换为整数或浮点数。

import numpy as np
from matplotlib.pyplot import *
from matplotlib.mlab import *

d1=csv2rec('stop-allrun-2.csv',delimiter=' ', names=['SubjID', 'SubjGrp', 'GoRT','SSRT','StopPoss'])

x = tuple(d1['SubjID'][1:])

subjid = tuple(map(int, d1['SubjID'][1:]))
gorts = tuple(map(float, d1['GoRT'][1:]))
ssrts = tuple(map(float, d1['SSRT'][1:]))

width = 0.35
ind = np.arange(len(x))

print(x, gorts, ind)

fig, ax = subplots()
rects1 = ax.bar(ind - width, subjid, width, color = 'y')
rects2 = ax.bar(ind, gorts, width, color = 'r')
rects3 = ax.bar(ind + width, ssrts, width, color = 'b')

ax.set_ylabel('msec')
ax.set_xticks(ind)
ax.set_xticklabels(x)

ax.legend((rects1[0], rects2[0], rects3[0]), ('SubjID', 'GoRT', 'SSRT'))

show()

This is the result: 结果如下:

It should work with both pyhon2 and python3. 它应该与pyhon2和python3一起使用。

Try this 尝试这个

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

d1=mlab.csv2rec('stop-allrun-2.csv',skiprows=1,delimiter='\t', names=['SubjID','SubjGrp', 'GoRT','SSRT','StopPoss'])#.astype('float')

x=d1['SubjID']

y=d1['GoRT']

plt.plot(x,y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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