1

I'm trying to read in a delimited csv called 'StabilityResults.csv' and create a bar plot of column 7 with x-labels from column 0. Getting the labels from column 0 is not a problem but reading the values from col7 into a list doesn't seem to work as a valid input in matplotlib. Is there a way to convert my list of values so they are readable in matplotlib??

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

res = csv.reader(open('StabilityResults.csv'), delimiter=',')
res.next() # do not read header

mut = []
tot = [] 
a = 0
width = 0.2

for col in res:
    mut.append(col[0])
    tot.append(col[7])
    a += 1

ind = arange(a)

p1 = plt.bar(ind,tot,width,color='r')
labs = plt.xticks(ind+width,mut)

plt.show()

I also reading column7 using numpy's genfromtxt function but this gave an array which also didnt work.

tot2 = np.genfromtxt('StabilityResults.csv', delimiter=',', dtype=None, names=True, deletechars=',', usecols=[7])
1
  • Normally with genfromtxt it should work. What error message dit you get? Commented Mar 30, 2011 at 14:21

3 Answers 3

1

You should convert the data to integer type (or float)

tot.append(int(col[7]))
Sign up to request clarification or add additional context in comments.

Comments

1

Ahh, should've spent more time. Similar to Manuel's answer, I just added a temporary holder to convert to a float in the for loop:

for col in res:
    tmp_tot = float(col[7])
    tot.append(tmp_tot)

Both ways work!

Cheers

Comments

0
mut.append(col[0])
tot.append(col[7])

You append the content of col list, which is text. You will have to convert it to int or float:

mut.append(float(col[0]))
tot.append(float(col[7]))

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.