天天看点

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简单生成直方图