- 安装可视化工具
matplotlib
- 一个数学会图库,可以绘制简单的图标,折线图,散点图。
- 检查是否安装了
matplotlib
安装matplotlib
,必须使用pip3
pip3 install --user matplotlib
- 可访问 https://matplotlib.org查看可绘制的图形
# 绘制折线图
import matplotlib.pyplot as plt # X轴对应的数据 x_value_list = [1, 2, 3, 4, 5] # Y轴对应的数据 squares = [1, 4, 9, 16, 25] plt.plot(x_value_list, squares, linewidth=5) # 设置图标的标题,并给坐标轴加上标签 plt.title("Square", fontsize=24) plt.xlabel("Value", fontsize=14) plt.ylabel("Square of Value", fontsize=14) # 设置刻度标记的大小 plt.tick_params(axis='both', labelsize=14) plt.show()
# 绘制散点图
- 在指定的xy坐标绘制一个点:
scatter(x,y)
import matplotlib.pyplot as plt x_list = list(range(101)) y_list = [x ** 2 for x in x_list] plt.scatter(x_list, y_list, c='red', edgecolors='green', s=10) plt.title("Square Numbers", fontsize=24) plt.xlabel("Value", fontsize=14) plt.ylabel("Square of Value", fontsize=14) plt.tick_params(axis='both', which='major', labelsize=14) # 横纵坐标的范围 plt.axis([0, 100, 0, 11000]) # 展示图片 # plt.show() # 保存图片到文件 plt.savefig('s.png', bbox_inches='tight')
# 模拟随机漫步(散点图)
- 生成随机x,y坐标点位
random_walk.py
from random import choice class RandomWalk: def __init__(self, num_points=5000): self.num_points = num_points self.x_values = [0] self.y_values = [0] def fill_walk(self): while len(self.x_values) < self.num_points: x_direction = choice([1, -1]) x_distance = choice([0, 1, 2, 3, 4]) x_step = x_direction * x_distance y_direction = choice([1, -1]) y_distance = choice([0, 1, 2, 3, 4]) y_step = y_direction * y_distance if x_step == 0 and y_step == 0: continue next_x = self.x_values[-1] + x_step next_y = self.y_values[-1] + y_step self.x_values.append(next_x) self.y_values.append(next_y)
- 根据生成的随机点位绘图
rw_visual.py
import matplotlib.pyplot as plt from data_show.walk.random_walk import RandomWalk while True: rw = RandomWalk() rw.fill_walk() plt.scatter(rw.x_values, rw.y_values, s=15) plt.show() con_str = input("continue(y/n)?\n") if con_str == 'y': continue else: break
- 结果
# 使用Pygal
绘制矢量图
- 安装
pip install --user pygal==1.7
- 需求描述:掷一个点数为1-6的六面骰子,掷1000次,统计每个点数出现的次数,并将统计结果绘制成柱状svg图
from random import randint import pygal class Die: """骰子""" def __init__(self, num_sides=6): """ 初始化方法 :param num_sides: 骰子的面数 """ self.num_sides = num_sides def roll(self): """ 掷骰子,Return random integer in range [a, b], including both end points. :return: """ return randint(1, self.num_sides) def draw(data_dict: dict): """ 绘图 :param data_dict: :return: """ hist = pygal.Bar() hist.title = "投掷1000次6面筛子的结果统计" hist.x_labels = data_dict.keys() hist.x_title = "点数" hist.y_title = "点数对应的次数" hist.add('6面骰子', data_dict.values()) # 导出问文件,扩展名必须为`.svg` hist.render_to_file('die_visual.svg') die = Die() result_list = [] # 掷骰子并保存结果 for i in range(1000): result_list.append(die.roll()) # 点数:出现次数 point_count_dict = {} # 分析每个点数出现的次数 for i in range(1, die.num_sides + 1): point_count_dict[i] = result_list.count(i) # 绘图 draw(point_count_dict)
- 结果:(使用浏览器打开svg文件)
- 各个点数出现的概率基本随机且相近
- 需求:同时投掷两个6面骰子,统计两个骰子的结果之和
from random import randint import pygal class Die: """骰子""" def __init__(self, num_sides=6): """ 初始化方法 :param num_sides: 骰子的面数 """ self.num_sides = num_sides def roll(self): """ 掷骰子,Return random integer in range [a, b], including both end points. :return: """ return randint(1, self.num_sides) def draw(data_dict: dict): """ 绘图 :param data_dict: :return: """ hist = pygal.Bar() hist.title = "投掷两个1000次6面筛子的结果统计" hist.x_labels = data_dict.keys() hist.x_title = "两个骰子的点数之和" hist.y_title = "点数对应的次数" hist.add('两个6面骰子', data_dict.values()) # 导出问文件,扩展名必须为`.svg` hist.render_to_file('die_visual.svg') die1 = Die() die2 = Die() result_list = [] # 掷骰子并保存结果 for i in range(1000): result_list.append(die1.roll() + die2.roll()) # 点数:出现次数 point_count_dict = {} # 分析每个点数出现的次数 for i in range(2, 2 * die1.num_sides + 1): point_count_dict[i] = result_list.count(i) # 绘图 draw(point_count_dict)
- 结果
- 出现点数之和为7的概率永远是最高的,因为7的组合方式最多~