数据可视化——两个不同的骰子

from die_1 import Die
import pygal

# 创建一个D6和一个D10
die_1 = Die()
die_2 = Die(10)

# 扔几次骰子,并将结果存储在一个列表中,扔1000次
results = []
for roll_num in range(1000):
    result = die_1.roll() + die_2.roll()
    results.append(result)

# 分析结果,每个点数出现的次数
# 先遍历可能的点数,再看点数的次数
# 求和两个骰子的点数
frequencies = []
for value in range(2, die_1.num_sides + die_2.num_sides + 1):
    frequency = results.count(value)
    frequencies.append(frequency)

print(str(frequencies)+'\n')
print(results)

# 对数据进行可视化操作 hist(直方图)
hist = pygal.Bar()

hist.title = "Results of rolling one D6 and one D6 1000 times"
hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16']
hist.x_title = "Result"
hist.y_title = "Frequencies of Result"

hist.add('D6 + D10', frequencies)
hist.render_to_file('dice_different_visual.svg') # 渲染成svg文件,SVG可以算是目前最最火热的图像文件格式了,它的英文全称为
# Scalable Vector Graphics,意思为可缩放的矢量图形

# 查看直方图的最简单方法就是打开web浏览器标签页,打开文件die_viasual.py(所在位置)。
# pygal让图标具有交互性,可以鼠标滑动过去凸显目标数据

在这里插入图片描述die_1.py的代码参考:(https://blog.csdn.net/weixin_44478378/article/details/100052261)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值