python中使用PyPI

來源:互聯網
上載者:User

在控制台中使用pip install pygal 安裝pygal外掛程式。參考網址:http://www.pygal.org/en/stable/installing.html


2. 在die.py檔案中,

from random import randintclass Die(): """表示一個骰子的類""" def __init__(self, num_sides=6):  """骰子預設為6面"""  self.num_sides = num_sides def roll(self):  """"返回一個位於1和骰子面數之間的隨機值"""  return randint(1, self.num_sides)
 
3. 在die_visual.py檔案中
 
import pygalfrom die import Die# 建立一個D6die = Die()# 擲幾次骰子,並將結果儲存在一個列表中results = []for roll_num in range(1000): result = die.roll() results.append(result) # 分析結果frequencies = []for value in range(1, die.num_sides+1): frequency = results.count(value) frequencies.append(frequency)print(frequencies)# 對結果進行可視化hist = pygal.Bar()hist.title = "Results of rolling one D6 1000 times."hist.x_labels = ['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')

4.代碼運行產生die_visual.svg檔案,在瀏覽器中開啟效果如下。
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.