0

I am trying to plot a histogram and a scatter plot for a particular column in a given csv file. I am new to programming and I got this code from a friend, and it apparently works but somehow I am getting this error. the code is:

import csv
import numpy as np
import matplotlib.pyplot as plt
f = open('Data for question 13.csv')
data = csv.reader(f)
Area = []; MajorAxisLength = []; MinorAxisLength = []; Perimeter = []
MinIntensity = []; MeanIntensity = []; MaxIntensity = []
header = [Area, MajorAxisLength, MinorAxisLength,Perimeter,MinIntensity,MeanIntensity,MaxIntensity]
for row in data:
    i = 1 
    for name in header:
        name.append(row[i])
        i = i + 1
plt.figure()
plt.hist(Area, bins=50) # error follows after this

Error:

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    plt.hist(Area, bins=50, alpha=0.5)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 2827, in hist
    stacked=stacked, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 8312, in hist
    xmin = min(xmin, xi.min())
  File "/Library/Python/2.7/site-packages/numpy-1.9.0-py2.7-macosx-10.9-intel.egg/numpy/core/_methods.py", line 29, in _amin
    return umr_minimum(a, axis, None, out, keepdims)
TypeError: cannot perform reduce with flexible type

I am unable to get rid of this error. The answer should be simple but since I am new I have no idea how to deal with it.

3
  • 1
    I can't reproduce this. What does your data file look like?
    – matsjoyce
    Commented Jan 7, 2015 at 17:49
  • is there a way I could share or upload the data file to look at how the data looks? I do not know if this would give you any idea and would help: It has 9 columns with headers and numerical data till approx 400 rows.
    – nik_10
    Commented Jan 10, 2015 at 14:40
  • Either paste the first few lines into the question (only ~10), or use eg. pastebin and put the link in the questtion
    – hitzg
    Commented Jan 10, 2015 at 14:43

3 Answers 3

1

You get the error because the input data is interpreted as string by the csv parser while hist requires numeric data. You should explicitly convert each row[i] before appending it.

1

Assuming you just want to plot some numerical data in your csv file, and that the data is numeric (not text) you could probably use the same method as mentioned here: How to read csv into record array in numpy?

Thus, your code might look like this:

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

data = np.genfromtxt('Data for question 13.csv')  # add more parameter info if neccessary like skipping header lines
plt.figure()
plt.hist(data[:,0], bins=50)  # Area, from OP, is column 0

Info on the genfromtxt function can be found here: http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html

2
  • Hey, I tried this too but I get the error for too many indices. IndexError: too many indices for array
    – nik_10
    Commented Jan 10, 2015 at 14:30
  • Then it sounds like your file might be empty. Do print(data.shape) (or print data.shape in Py2) and if it's (0,0) there's something wrong with your file. You could also plot it in Excel to know what you're looking for. There's enough clues in the errors you've gotten from mine and @hitzg's answers that you should be able to solve the problem. IndexError? The data isn't there/big enough. ValueError? Look at the string; what is it? Is it convertable?
    – James
    Commented Jan 10, 2015 at 16:16
0

I'm not 100% sure, as I don't have your data file available. But I think row[i] is a string (and not an integer or float). And you can use enumerate to skip the first line. So this should solve the problem:

for n,row in enumerate(data):
    if n > 0:
        i = 1 
        for name in header:
            name.append(float(row[i]))
            i = i + 1
3
  • I tried this but it still gives me an error: ValueError: could not convert string to float: Area
    – nik_10
    Commented Jan 10, 2015 at 14:28
  • 1
    If the first line of your file includes the headers you have to skip it. It is extremely difficult to help you without the file that you're working with. And I think @James' answer is doing what you want more directly. You should read the documentation he linked.
    – hitzg
    Commented Jan 10, 2015 at 14:35
  • I tried this again with skipping the header after reading the documentation and it did work. Thanks for the help.
    – nik_10
    Commented Jan 13, 2015 at 19:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.