Frontier
For charts that need to be displayed on a different size screen, consider using Pygal to generate them, because they will automatically scale to fit the viewer's screen, so they will look good on any device. Next I will talk about the Pygal module generation line, the basic usage of the histogram, with the case of the book dice more in-depth understanding of the use of Pygal module, for the creation of Pygal other graphics in fact the method is similar to the actual use of what kind of graphics to go to the official website query, the official website has a lot of graphic created sample code, Pygal Gallery Website Link: http://www.pygal.org/
As shown in the figure below (there is a code, you know how to play it again almost):
Pygal Drawing a line chart
Drawing the line diagram is very simple, it is important to note that finally we use Render_to_file to render the chart as an SVG file, and the browser opens the SVG file to view the generated chart.
The code is as follows:
# 导入pygal可视化模块import pygalline_chart = pygal.Line() # 创建一个线图的实例化对象line_chart.title = ‘Browser usage evolution (in %)‘ # 设置标题line_chart.x_labels = map(str, range(2002, 2013)) # 设置X轴标签,从2002年到2013年# 下面是添加四条由11个点连成的线line_chart.add(‘Firefox‘, [None, None, 0, 16.6, 25, 31, 36.4, 45.5, 46.3, 42.8, 37.1])line_chart.add(‘Chrome‘, [None, None, None, None, None, None, 0, 3.9, 10.8, 23.8, 35.3])line_chart.add(‘IE‘, [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1])line_chart.add(‘Others‘, [14.2, 15.4, 15.3, 8.9, 9, 10.4, 8.9, 5.8, 6.7, 6.8, 7.5])line_chart.render_to_file(‘bar_chart.svg‘) # 将图像保存为SVG文件,可通过浏览器查看
The results of the operation are as follows:
Pygal draw a horizontal line chart
The basic usage is similar to drawing a line chart with the following code:
# 导入pygal可视化模块import pygalline_chart = pygal.HorizontalLine() # 创建一个水平线图的实例化对象line_chart.title = ‘Browser usage evolution (in %)‘ # 设置标题line_chart.x_labels = map(str, range(2002, 2013)) # 注意,这里的是水平线图,那么X轴就变为Y轴,Y轴变为X轴,所以这里map返回的值应用于Y轴# 下面是添加四条由11个点连成的线line_chart.add(‘Firefox‘, [None, None, 0, 16.6, 25, 31, 36.4, 45.5, 46.3, 42.8, 37.1])line_chart.add(‘Chrome‘, [None, None, None, None, None, None, 0, 3.9, 10.8, 23.8, 35.3])line_chart.add(‘IE‘, [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1])line_chart.add(‘Others‘, [14.2, 15.4, 15.3, 8.9, 9, 10.4, 8.9, 5.8, 6.7, 6.8, 7.5])line_chart.range = [0, 100] # 设置X轴的范围line_chart.render_to_file(‘bar_chart.svg‘) # 将图像保存为SVG文件,可通过浏览器查看
The results of the operation are as follows:
Pygal plotting histograms
The basic usage is similar to the above, with the following code:
# 导入pygal可视化模块import pygalfrequency = [10, 20, 30, 40, 50, 60]bar = pygal.Bar() # 创建一个直方图的实例化对象bar.title = ‘test‘ # 设置标题bar.x_labels = [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘]bar.x_title = "Result"bar.y_title = "Frequency of Result"bar.add(‘D‘, frequency)bar.render_to_file(‘bar_chart.svg‘) # 将图像保存为SVG文件,可通过浏览器查看
The results of the operation are as follows:
Throw a dice with Pygal simulation
The following steps are required to complete the DICE project:
1. Create Die dice class to simulate the human roll dice process
2. The number of points after each roll of the dice, and a little number corresponding to the occurrence of the times are saved in the results and frequencies list
3. Draw the histogram according to the data results and frequencies list obtained in the second step
The code is as follows:
(1) Create Die dice class to simulate the human roll dice process
Create a die.py file under the project directory with the following file code:
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)
(2) Save the data to the results and frequencies lists and use the Pygal to draw the histogram based on the data
Create a dice_visual.py file under the project directory with the following code:
# 下面是掷一个六面骰子的案例from die import Dieimport pygal# 实例化一个Die类对象die = Die()results = []for roll_num in range(1000): result = die.roll() # 调用实例化对象的roll方法随机生成一个数字,在1-6之间的数字模拟掷骰子 results.append(result) # 将结果放入results列表frequencies = []# 将实验的结果数据统计出每个数字出现的次数for value in range(1, die.num_sides + 1): frequency = results.count(value) frequencies.append(frequency)# 绘制直方图# 实例化一个bar对象,对该对象的title、x_labels、x_title、y_title属性设置相当于在直方图设置。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 = "Frequencies of result"hist.add(‘D6‘, frequencies)hist.render_to_file(‘die_visual.svg‘)
The results of the operation are as follows:
As can be seen from the above chart, throw a D6 dice, each point appears the likelihood of nearly equal, if the number of dice more, then the probability of each point appears more close to 6 1.
Throw two dice with Pygal simulation
The previous case is to throw a dice, more simple. This case is to throw two dice, get more points, the results distribution is also different. We created two dice to simulate the simultaneous roll of two dice, each time we throw two dice, we will add two dice points, and store the results in results. Finally, the histogram is drawn using the Pygal module.
Modify the dice_visual.py file code as follows:
# 下面是掷两个六面骰子的案例from die import Dieimport pygal# 实例化两个个Die类对象die_1 = Die()die_2 = Die()results = []for roll_num in range(1000): result = die_1.roll() + die_2.roll() # 将两次模拟掷骰子的值相加 results.append(result) # 将结果放入results列表frequencies = []max_result = die_1.num_sides + die_2.num_sides# 将实验的结果数据统计出每个数字出现的次数for value in range(2, max_result + 1): # 两个骰子相加最小也是2 frequency = results.count(value) frequencies.append(frequency)# 绘制直方图# 实例化一个bar对象,对该对象的title、x_labels、x_title、y_title属性设置相当于在直方图设置。hist = pygal.Bar()hist.title = "Results of rolling two D6 dice 1000 times"hist.x_labels = list(range(2, max_result + 1))hist.x_title = "Result"hist.y_title = "Frequencies of result"hist.add(‘D6 + D6‘, frequencies)hist.render_to_file(‘dice_visual1.svg‘)
The results of the operation are as follows:
From the chart above you can see the two D6 dice, the total number of points is 2 or 12 is the least possible, and the total number of 7 is the most likely, because in 6 cases (1 and 6,2 and 5,3 and 3,5 and 2,6 and 1) The total number of points are 7.
Use Pygal to simulate a dice that throws two different numbers of faces
Create a 6-sided dice and 10-sided dice, then roll two dice 50,000 times at a time.
Re-modify the dice_visual.py file code as follows:
# 下面是掷两个面数不同的骰子案例from die import Dieimport pygal# 实例化两个Die类对象die_1 = Die()die_2 = Die(10) # 注意这里传入10results = []for roll_num in range(50000): result = die_1.roll() + die_2.roll() results.append(result) # 将结果放入results列表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)# 绘制直方图# 实例化一个bar对象,对该对象的title、x_labels、x_title、y_title属性设置相当于在直方图设置。hist = pygal.Bar()hist.title = "Results of rolling a D6 and a D10 50,000 times"hist.x_labels = list(range(2, max_result + 1))hist.x_title = "Result"hist.y_title = "Frequencies of result"hist.add(‘D6 + D10‘, frequencies)hist.render_to_file(‘dice_visual2.svg‘)
The results of the operation are as follows:
A discussion on the Pygal module of Python real-data visualization (Basic article)