如何为类属性PyPlot绘制直方图

时间:2016-03-02 02:54:52

标签: python matplotlib

我想尝试根据我给他们的随机分数属性绘制一个“团队”对象数组的直方图,如下所示:

class Team(object):
    def __init__(self):
        self.score = random.randint(1,1000)

#What goes after this?

1 个答案:

答案 0 :(得分:1)

import matplotlib.pyplot as plt
import random

class Team(object):
    def __init__(self):
        self.score = random.randint(1,1000)

def plot_hist(teams, bins=40):
    scores = [team.score for team in teams]
    plt.hist(scores, bins)
    plt.show()

teams = [Team() for _ in range(1000)]

plot_hist(teams)