天天看點

Pygal簡單生成直方圖

Pygal是Python可視化包,來生成可縮放的矢量圖形檔案 

生成的其實是一個xml檔案,需要用你的web浏覽器來打開

下面舉一個模拟擲骰子的例子

模拟投擲一個六面骰子

# -*- coding: utf-8 -*-
# @Author: Clarence
# @Date:   2018-03-24 14:40:28
# @Last Modified by:   Clarence
# @Last Modified time: 2018-03-24 14:41:51
"""
有可能大家在使用的時候會hist.render_to_file('die_visual.svg')儲存到檔案中會報錯
pygal ValueError: Invalid PI name 'b'xml''

stackoverflow大神解答:
Try running pip install --upgrade pygal. If it doesn't help use pip list --outdated 
or pip list to check the installed versions (pygal version should be at least 2.0.11).
Edit your post and include the output as text (use Ctrl+Shift+C to copy from the terminal).
可以直接在dos中輸入pip install --upgrade pygal
Collecting pygal
  Downloading pygal-2.4.0-py2.py3-none-any.whl (127kB)
    100% |████████████████████████████████| 133kB 353kB/s
Installing collected packages: pygal
  Found existing installation: pygal 1.7.0
    Uninstalling pygal-1.7.0:
      Successfully uninstalled pygal-1.7.0
Successfully installed pygal-2.4.0
這樣就更新成功了
"""

from die import Die
import pygal

# 建立一個六面骰子
die = 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)


# 對結果進行可視化
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)
# 實際上會生成一個xml檔案,大家浏覽器可以打開或者直接用IDE打開
hist.render_to_file('die_visual.svg')
           
Pygal簡單生成直方圖

下面模拟同時投擲兩個六面篩子繪制機率密度圖像

"""
同時投擲兩個骰子
"""
import pygal

from die import Die 

# 建立兩個六面骰子D6
die_1 = Die()
die_2 = Die()

# 投擲篩子多次,并将結果存儲到一個清單中
results = []
for roll_num in range(1000):
	result = die_1.roll() + die_2.roll()
	results.append(result)

# 分析結果
frequencies = []
max_result = die_1.num_sides + die_2.num_sides
for value in range(2, max_result + 1):
	frequency = results.count(value)
	frequencies.append(frequency)

# 可視化結果
hist = pygal.Bar()

hist.title = "Results of rolling two D6 dice 1000 time."
hist.x_labels = [str(i) for i in range(2, max_result + 1)]
hist.x_title = "Result"
hist.y_title = "frequency of Result"

hist.add("D6 + D6", frequencies)
hist.render_to_file('dice_visual.svg')
           
Pygal簡單生成直方圖

下面模拟同時投擲一個6面骰子和一個10面骰子

"""
建立一個6面骰子和一個10面骰子,投擲5000次
"""
import pygal
from die import Die  

die_1 = Die()
die_2 = Die(10)


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

frequencies = []
max_result = die_1.num_sides + die_2.num_sides
for value in range(2, max_result + 1):
	frequency = results.count(value)
	frequencies.append(frequency)

hist = pygal.Bar()

hist.title = "Results of rolling a D6 and D10 50,000 times."
hist.x_labels = [str(i) for i in range(2, max_result + 1)]
hist.x_title = "Result"
hist.y_title = "frequency of Result"

hist.add('D6 + D10', frequencies)
hist.render_to_file('different.svg')
           
Pygal簡單生成直方圖