from random import randint
import pygal
class Die():
    def __init__(self,num_sides=6):
        self.num_sides=num_sides

    def roll(self):
       return randint(1,self.num_sides)

    def get_count(self,nums_sides):
        cc={}
        lists=[]
        for num in range(1,nums_sides+1):
            the=nums.count(num)
            lists.append(the)
            cc[num]=the
        print(lists)
        for item,val in cc.items():
            print(str(item)+"出现了"+str(val)+"")
        return  lists

die=Die()
nums=[]
for i in  range(100):
   num= die.roll()
   nums.append(num)
print(nums)
lists=die.get_count(die.num_sides)
#绘制柱形图
gal=pygal.Bar()
gal.title="title"
gal.x_labels=[i for i in range(1,die.num_sides+1)]
gal.x_title="x title"
gal.y_title="y title"
gal.add("柱形图的标题",lists)#添加显示的数据
gal.render_to_file("aaa.svg")#存放在文件

 

posted on 2018-03-16 11:41  ghgxm520  阅读(456)  评论(0编辑  收藏  举报