日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python 使用matplotlib模塊模擬擲骰子

系統 1770 0

擲骰子

骰子類

            
# die.py 骰子類模塊
from random import randint
class Die():
  """骰子類"""
  def __init__(self, num_sides=6):
    """默認六面的骰子"""
    self.num_sides = num_sides
  def roll(self):
    """擲骰子的方法"""
    return randint(1, self.num_sides)
          

折線圖擲骰子

            
# die_visual_plot.py 使用plot可視化骰子
import matplotlib.pyplot as plt
from die import Die
# Initialization
die = Die()
# 擲骰子
results = [die.roll() for x in range(1000)]
# 分析結果
frequencies = [results.count(x) for x in range(1, die.num_sides+1)]
# 可視化結果
values = [x for x in range(1, die.num_sides+1)]
plt.plot(values, frequencies, linewidth=2, marker='o', markerfacecolor='yellow', markersize=5, color='b')
# 設置圖表
plt.title('Roll a die using matplotlib', fontsize=24)
plt.xlabel('Value', fontsize=14)
plt.ylabel('Frequency', fontsize=14)
# 顯示數據
for x, y in zip(values, frequencies):
  # 將y數據加載到(x,y)位置
  plt.text(x, y, y, fontsize=12, color='red', ha='center', va='bottom')
# 顯示結果
plt.show()
          

散點圖擲骰子

            
# die_visual_scatter.py 使用scatter可視化骰子
import matplotlib.pyplot as plt
from die import Die
# Initialization
die = Die()
# 擲骰子
results = [die.roll() for x in range(1000)]
# 分析結果
frequencies = [results.count(x) for x in range(1, die.num_sides+1)]
# 可視化結果
values = [x for x in range(1, die.num_sides+1)]
plt.scatter(values, frequencies, c=frequencies, cmap=plt.cm.Blues, edgecolor='none', s=10)
# 設置圖表
plt.title('Roll a die using matplotlib', fontsize=24)
plt.xlabel('Value', fontsize=14)
plt.ylabel('Frequency', fontsize=14)
# 顯示數據
for x, y in zip(values, frequencies):
  # 將y數據加載到(x,y)位置
  plt.text(x, y, y, fontsize=12, color='red', ha='center', va='bottom')
# 顯示結果
plt.show()
          

Python 使用matplotlib模塊模擬擲骰子_第1張圖片 ? Python 使用matplotlib模塊模擬擲骰子_第2張圖片

總結

以上所述是小編給大家介紹的Python 使用matplotlib模塊模擬擲骰子,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 天台县| 朝阳市| 马公市| 屯留县| 郑州市| 饶平县| 长沙县| 高邮市| 青河县| 阿克苏市| 鹤山市| 黄浦区| 法库县| 龙里县| 分宜县| 日土县| 南澳县| 东乡| 海阳市| 井研县| 营口市| 邵武市| 得荣县| 嘉定区| 五大连池市| 石台县| 兴国县| 红桥区| 阿拉善盟| 噶尔县| 长顺县| 沂源县| 平安县| 乌鲁木齐县| 双桥区| 阆中市| 辽阳市| 海晏县| 泊头市| 宁河县| 莲花县|