《python编程从入门到实践》15-10练习

练习使用本章介绍的两个库:尝试使用matplotlib通过可视化来模拟掷骰子的情况,并尝试使用Pygal通过可视化来模拟随机漫步的情况

下面仅列出相关程序,导入的类为书中所写的类

from die import Die
import matplotlib.pyplot as plt 
import matplotlib.patches as mpatches


die_1 = Die()
die_2 = Die(10)

results = []
for roll_num in range(50000):
    #result = die_1.roll()
    result = die_1.roll() + die_2.roll()
    results.append(result)

frequencies =[]
#max_results = die_1.num_sides 
max_results = die_1.num_sides + die_2.num_sides
X_Range = list(set(results))
for value in X_Range:
    frequency = results.count(value)
    frequencies.append(frequency)

plt.bar(X_Range,frequencies,color = 'Pink',align='center',label = 'D6+D6')
plt.xticks(X_Range)


plt.xlabel("Results",fontsize = 8)
plt.ylabel("Frequencies", fontsize = 8)

#图例
pink_patch = mpatches.Patch(color='Pink', 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值