0
votes

I'm absolutely stumped as to why I'm getting this error. Any help will be appreciated!

This is the error info:

File "C:/Python27/Scripts/Lab08realdeal.py", line 23, in plt.hist(count,range=20,color = 'red')

File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2896, in hist stacked=stacked, **kwargs)

File "C:\Python27\lib\site-packages\matplotlib\axes_axes.py", line 5603, in hist raise ValueError("color kwarg must have one color per dataset")

ValueError: color kwarg must have one color per dataset

import csv
import matplotlib.pyplot as plt

def loadContaminantFrequencies(contaminant, fileInfo):
 count= 0  
 for line in fileInfo: 
    if contaminant == line[0]:
      count = count+1

 return count 

ifile = open('air_samples.csv',"rb")
fileInfo = csv.reader(ifile)
count = ("Benzene", fileInfo)
counts = [count,count]


plt.hist(count,range=20,color = 'red')
plt.xlabel("CountOfChemical")
plt.ylabel("Frequency")
plt.axes([0,3000,0,1])
plt.show()
2
This may come from a type problem, and you may want to look at: stackoverflow.com/a/60259377/12910854Arnold Vialfont

2 Answers

0
votes

hist() takes either an array or a sequence of arrays as a parameter. If it gets a sequence, it tries to plot several histograms, one for each array in the sequence. Your first parameter, count, is a 2-element tuple, which is recognized as 2 separate datasets. Therefore hist() wants color kwarg to also have 2 elements, but it can only find one, 'red', hence the error.

I'm not sure what are you trying to do, but perhaps the correct call is

plt.hist(fileInfo,range=20,color = 'red')

(because I can't imagine how one would plot a histogram of the string "Benzene")

Edit: My answer assumed that fileInfo is something histogram-able, which it apparently isn't (that's why you should always try to post self-contained examples). Not that it matters in terms of the original question anyway, the answer does not depend on it.

According to the docs of the csv module, you need to extract the data first. Again, since I don't have all the information, I'll assume that the data in your CSV file is already good to go (that is, has one column of floats).

fileInfo = csv.reader(ifile)
count = numpy.array([float(row[0]) for row in fileInfo])

plt.hist(count,range=20,color = 'red')

Please try to resolve any following problems yourself, or at least create a separate question.

-1
votes

If you just delete attribute color = 'red' everything will probably work ok.