0

I have a naive question about plotting csv data with python.

I have a csv file looks like:

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: enter image description here

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

Is there anything I can do to improve this script?

1
  • 2
    You probably want to replace y=['GoRT'] with y=d1['GoRT']. Commented Jun 16, 2017 at 14:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.