python数据可视化pygal模拟掷骰子如何实现
这篇文章主要介绍“python数据可视化pygal模拟掷骰子如何实现”,在日常操作中,相信很多人在python数据可视化pygal模拟掷骰子如何实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”python数据可视化pygal模拟掷骰子如何实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
可视化包Pygal生成可缩放矢量图形文件
可以在尺寸不同的屏幕上自动缩放,显示图表
#安装pygal pip install pygal ''' 想要了解Pygal可生成什么样的图表,可访问http://www.pygal.org/ 单击document,点击chart types,每个示例都包含源代码 ''' from random import randint #创建一个骰子的类 class Die(): def __init__(self,num_sides = 6): self.num_sides = num_sides def roll(self): #返回一个位于1和骰子面数之间的随机值 return randint(1, self.num_sides) #掷骰子 die = Die() #创建一个列表,将结果存储在一个列表中 results = [] #投100次 for roll_num in range(100): result = die.roll() results.append(result) print(results) [3, 4, 2, 2, 6, 5, 5, 5, 2, 3, 1, 4, 3, 2, 1, 2, 3, 6, 6, 5, 5, 3, 2, 3, 1, 1, 4, 1, 4, 6, 1, 6, 2, 3, 4, 6, 2, 5, 5, 1, 6, 1, 5, 4, 3, 3, 4, 5, 6, 3, 5, 1, 4, 3, 5, 6, 6, 6, 4, 6, 5, 6, 5, 4, 6, 3, 1, 4, 1, 4, 2, 1, 1, 4, 4, 4, 2, 3, 1, 4, 6, 2, 1, 5, 6, 2, 2, 6, 6, 3, 6, 2, 6, 6, 4, 4, 2, 1, 1, 6]
分析结果,计算每个点数出现的次数
frequencies = [] for value in range(1, die.num_sides+1): frequency = results.count(value) frequencies.append(frequency) print(frequencies) [10, 23, 13, 9, 26, 19]
绘制直方图
import pygal hist = pygal.Bar() hist.title = 'results of rolling one d6 100 times' hist.x_lables = ['1', '2', '3', '4', '5', '6'] hist.x_title = 'result' hist.y_title = 'frequency of result' hist.add('d6', frequencies) #将图渲染为SVG文件,需要打开浏览器,才能查看生成的直方图 hist.render_to_file('die_visual.svg')
同时投掷两个骰子
from random import randint #创建一个骰子的类 class Die(): def __init__(self,num_sides = 6): self.num_sides = num_sides def roll(self): #返回一个位于1和骰子面数之间的随机值 return randint(1, self.num_sides) #掷骰子 die1 = Die() die2 = Die() #创建一个列表,将结果存储在一个列表中 results = [] #投100次 for roll_num in range(100): result = die1.roll() + die2.roll() results.append(result) print(results) #分析结果,计算每个点数出现的次数 frequencies = [] max_result = die1.num_sides + die2.num_sides for value in range(1, max_result+1): frequency = results.count(value) frequencies.append(frequency) print(frequencies) #绘制直方图 import pygal hist = pygal.Bar() hist.title = 'results of rolling one d6 dice 100 times' hist.x_lables = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'] hist.x_title = 'result' hist.y_title = 'frequency of result' hist.add('d6 + d6', frequencies) #将图渲染为SVG文件,需要打开浏览器,才能查看生成的直方图 hist.render_to_file('die_visual.svg') [4, 7, 4, 5, 8, 4, 3, 6, 8, 9, 8, 11, 9, 11, 8, 8, 5, 6, 10, 5, 11, 7, 4, 3, 12, 12, 7, 2, 4, 9, 9, 5, 7, 10, 4, 7, 4, 6, 5, 6, 7, 2, 7, 9, 7, 6, 11, 5, 9, 6, 11, 4, 8, 10, 7, 9, 5, 4, 3, 7, 4, 10, 5, 7, 2, 6, 4, 2, 2, 5, 5, 9, 6, 3, 6, 10, 12, 7, 4, 11, 8, 6, 10, 5, 7, 5, 5, 7, 9, 4, 11, 6, 7, 8, 6, 11, 6, 4, 3, 12] [0, 5, 5, 14, 13, 13, 15, 8, 9, 6, 8, 4]
同时投掷两个面数不同骰子
from random import randint #创建一个骰子的类 class Die(): def __init__(self,num_sides = 6): self.num_sides = num_sides def roll(self): #返回一个位于1和骰子面数之间的随机值 return randint(1, self.num_sides) #掷骰子 die1 = Die() die2 = Die(10) #创建一个列表,将结果存储在一个列表中 results = [] #投100次 for roll_num in range(100): result = die1.roll() + die2.roll() results.append(result) print(results) #分析结果,计算每个点数出现的次数 frequencies = [] max_result = die1.num_sides + die2.num_sides for value in range(1, max_result+1): frequency = results.count(value) frequencies.append(frequency) print(frequencies) #绘制直方图 import pygal hist = pygal.Bar() hist.title = 'results of rolling one d10 dice 100 times' hist.x_lables = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14','15','16'] hist.x_title = 'result' hist.y_title = 'frequency of result' hist.add('d6 + d10', frequencies) #将图渲染为SVG文件,需要打开浏览器,才能查看生成的直方图 hist.render_to_file('die_visual.svg') [5, 3, 6, 13, 8, 9, 10, 11, 11, 4, 5, 14, 11, 10, 11, 8, 14, 12, 16, 8, 9, 11, 7, 11, 9, 2, 8, 9, 9, 10, 7, 8, 12, 11, 8, 12, 9, 9, 10, 11, 8, 14, 10, 12, 10, 7, 12, 5, 4, 8, 6, 7, 7, 11, 9, 16, 6, 13, 6, 10, 6, 7, 16, 9, 14, 5, 7, 12, 8, 9, 11, 11, 6, 11, 5, 8, 11, 16, 4, 10, 5, 10, 13, 4, 9, 9, 11, 9, 11, 13, 7, 13, 13, 5, 5, 4, 5, 3, 12, 14] [0, 1, 2, 5, 9, 6, 8, 10, 13, 9, 15, 7, 6, 5, 0, 4]
到此,关于“python数据可视化pygal模拟掷骰子如何实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注蜗牛博客网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。
评论