简体   繁体   中英

How do I generate a histogram from a list of values using matplotlib?

so I've been trying to plot a histogram using python with mathplotlib. So I've got two datasets, basically the heights of a sample of men and women as a list in python, imported off a csv file.

The code that I'm using:

import csv
import numpy as np
from matplotlib import pyplot as plt
men=[]
women=[]

with open('women.csv','r') as f:
    r1=csv.reader(f, delimiter=',')
    for row in r1:
        women+=[row[0]]

with open('men.csv','r') as f:
    r2=csv.reader(f, delimiter=',')
    for row in r2:
        men+=[row[0]]


fig = plt.figure()
ax = fig.add_subplot(111)

numBins = 20
ax.hist(men,numBins,color='blue',alpha=0.8)
ax.hist(women,numBins,color='red',alpha=0.8)
plt.show()

and the error that I get:

Traceback (most recent call last):
  File "//MEME/Users/Meme/Miniconda3/Lib/idlelib/test.py", line 22, in <module>
    ax.hist(men,numBins,color='blue',alpha=0.8)
  File "\\MEME\Users\Meme\Miniconda3\lib\site-packages\matplotlib\__init__.py", line 1811, in inner
    return func(ax, *args, **kwargs)
  File "\\MEME\Users\Meme\Miniconda3\lib\site-packages\matplotlib\axes\_axes.py", line 5983, in hist
    raise ValueError("color kwarg must have one color per dataset")
ValueError: color kwarg must have one color per dataset

NOTE:assume your files contain multiple lines (comma separated) and the first entry in each line is the height.

The bug is when you append "data" into the women and men list. row[0] is actually a string. Hence matplotlib is confused. I suggest you run this code before plotting (python 2):

import csv
import numpy as np
from matplotlib import pyplot as plt
men=[]
women=[]
import pdb;
with open('women.csv','r') as f:
    r1=csv.reader(f, delimiter=',')
    for row in r1:
        women+=[(row[0])]

with open('men.csv','r') as f:
    r2=csv.reader(f, delimiter=',')
    for row in r2:
        men+=[(row[0])]


fig = plt.figure()
ax = fig.add_subplot(111)
print men
print women
#numBins = 20
#ax.hist(men,numBins,color='blue',alpha=0.8)
#ax.hist(women,numBins,color='red',alpha=0.8)
#plt.show()

A sample output will be

['1','3','3']
['2','3','1']

So in the loops, you just do a conversion from string into float or integers eg women += [float(row[0])] and men += [float(row[0])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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