码农之家
这篇文章主要知识点是关于Python编程,从入门到实践,生成数据,Python编程:从入门到实践,《Python编程:从入门到实践》第十一章:测试代码 实例详解Python编程实现生成特定范围内不重复多个随机数的2种方法 Python小程序编程实现一键自动整理文件解压文件 《Python编程:从入门到实践》第十二章:武装飞船 《Python编程:从入门到实践》DEMO实例代码 的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书
数字的三次方被称为其立方。请绘制一个图形,显示前5个整数的立方值,在绘制一个图形,显示前面5000个整数的立方值。
前5个整数的立方值:
import matplotlib.pyplot as plt x_values = [1,2,3,4,5] y_values = [1,8,27,64,125] plt.title("Li Fang ") plt.plot(x_values,y_values,linewidth = 5) plt.xlabel("Value",fontsize = 14) plt.ylabel("Square of Value",fontsize = 14) plt.tick_params(axis = 'both' , labelsize = 14) plt.show()
结果:
前面5000个整数的立方值:
import matplotlib.pyplot as plt x_values = list(range(1,50001)) y_values = [x**3 for x in x_values] plt.title("Li Fang ") plt.scatter(x_values,y_values,linewidth = 5,edgecolor = 'none') plt.xlabel("Value",fontsize = 14) plt.ylabel("Square of Value",fontsize = 14) plt.tick_params(axis = 'both' , labelsize = 14) plt.show()
结果:
给你前面的立方图指定颜色映射
import matplotlib.pyplot as plt x_values = [1,2,3,4,5] y_values = [1,8,27,64,125] plt.title("Li Fang ") plt.plot(x_values,y_values,linewidth = 5,c = 'red') plt.xlabel("Value",fontsize = 14) plt.ylabel("Square of Value",fontsize = 14) plt.tick_params(axis = 'both' , labelsize = 14) plt.show()
结果:
修改rw_visual.py,将其中的plt.scatter()替换为plt.plot(),为模拟花粉在水滴表面的运动路径,向plt.plot()传递rw.x_values和rw.y_values,并指定实参值linewidth。使用5000个点而不是50000个点。
rw_visual.py
import matplotlib.pyplot as plt from random_walk import RandomWalk rw = RandomWalk() rw.fill_walk() plt.plot(rw.x_values,rw.y_values,linewidth = 2) plt.show()
random_walk.py
from random import choice class RandomWalk(object): """docstring for 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 else: 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)
结果:
在类RandomWalk中,x_step 和 y_step 是根据相同的条件生成的:从列表【1,-1】中随机地选择方向,并经列表【0,1,2,3,4】中随机地选择距离。请修改这些列表中的值,看看对随机漫步路径有何影响。长沙使用更长距离选择列表,如0~8;或者将-1从x或y方向列表中删除
方法fill_walk()很长。请新建一个名为get_step()的方法,用于确定每次漫步的距离和方向,并计算这次漫步将如何移动。然后,在fill_work() 中调用get_step()两次:
x_step = self.get_step()
y_step = self.get_step()
通过这样的重构,可缩小fill_walk() 的规模,让这个方法阅读和理解起来更容易
from random import choice class RandomWalk(object): """docstring for 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_step = self.get_step() y_step = self.get_step() if x_step == 0 and y_step ==0: continue else: next_x = self.x_values[-1] + x_step next_y = self.y_values[-1] + y_step self.x_values.add(next_x) self.y_values.add(next_y) def get_step(self): direction = choice([1,-1]) distance = choice([0,1,2,3,4]) step = direction * distance return step
请修改die.py 和 dice_visual.py,将用来设置 hist.x_labels 值的列表替换为字节自动生成这种列表的循环。如果你熟悉列表解析,可尝试将 die_visual.py 和 dice_visual.py 中的其他 for 循环也替换为列表解析。
die_visual.py
from die import Die import pygal def num2char(x): return str(x) die1 = Die() die2 = Die() results = [] for roll_num in range(1000): result = die1.roll() + die2.roll() results.append(result) # print(results) frequencies = [] max_result = die1.num_sides + die2.num_sides for value in range(1,max_result+1): frequency = results.count(value) frequencies.append(frequency) hist = pygal.Bar() hist.title = "Result of rolling me D6 1000 times." hist.x_labels = list(map(num2char,list(range(1,max_result+1)))) print(hist.x_labels) hist.x_title = "Result" hist.y_title = "Frequency of Result" hist.add('D6+D6',frequencies) hist.render_to_file('dice1_visual.svg')
请模拟同时透支两个8面骰子1000次的结果。逐渐增加投掷骰子的次数,知道系统不堪重负为止。
die.py
from random import randint class Die(object): """docstring for Die""" def __init__(self, num_sides = 8): self.num_sides = num_sides def roll(self): return randint(1,self.num_sides)
die_visual.py
from die import Die import pygal die1 = Die() die2 = Die() results = [] for roll_num in range(1000): result = die1.roll() + die2.roll() results.append(result) frequencies = [] max_result = die1.num_sides + die2.num_sides for value in range(2,max_result+1): frequency = results.count(value) frequencies.append(frequency) hist = pygal.Bar() hist.title = "Result of rolling me D8 1000 times." hist.x_labels = ['2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'] hist.x_title = "Result" hist.y_title = "Frequency of Result" hist.add('D8+D8',frequencies) hist.render_to_file('dice1_visual.svg')
结果:
如果你同时掷三个D6骰子,可能得到的最小点数为3,而最大点数为18,。请通过可视化展示同时掷三个D6骰子的结果
die_visual.py
from die import Die import pygal die1 = Die() die2 = Die() die3 = Die() results = [] for roll_num in range(1000): result = die1.roll() + die2.roll() + die3.roll() results.append(result) frequencies = [] max_result = die1.num_sides + die2.num_sides + die3.num_sides for value in range(2,max_result+1): frequency = results.count(value) frequencies.append(frequency) hist = pygal.Bar() hist.title = "Result of rolling me D6 1000 times." hist.x_labels = ['2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18'] hist.x_title = "Result" hist.y_title = "Frequency of Result" hist.add('D6+D6+D6',frequencies) hist.render_to_file('dice1_visual.svg')
结果:
同时掷两个骰子时,通常将它们的点数相加。通过可视化展示将两个骰子的点数相乘的结果
from die import Die import pygal def num2char(x): return str(x) die1 = Die() die2 = Die() results = [] for roll_num in range(1000): result = die1.roll() * die2.roll() results.append(result) # print(results) frequencies = [] max_result = die1.num_sides * die2.num_sides for value in range(1,max_result+1): frequency = results.count(value) frequencies.append(frequency) hist = pygal.Bar() hist.title = "Result of rolling me D6 1000 times." hist.x_labels = list(map(num2char,list(range(1,max_result+1)))) print(hist.x_labels) hist.x_title = "Result" hist.y_title = "Frequency of Result" hist.add('D6*D6',frequencies) hist.render_to_file('dice1_visual.svg')
结果:
尝试使用 matplotlib 通过可视化来模拟值骰子的情况。
die_scatter.py
import matplotlib.pyplot as plt from die import Die die1 = Die() die2 = Die() results = [] for roll_num in range(1000): result = die1.roll() + die2.roll() results.append(result) # print(results) frequencies = [] max_result = die1.num_sides + die2.num_sides for value in range(1,max_result+1): frequency = results.count(value) frequencies.append(frequency) print(frequencies) plt.plot(list(range(1,max_result+1)),frequencies,linewidth = 2) plt.title("Result of rolling me D6 1000 times.") plt.xlabel ("Result",fontsize = 14) plt.ylabel("Square of D6 1000 times") plt.show()
结果:
random_walk_Pygal.py
以上就是本次给大家分享的全部知识点内容总结,大家还可以在下方相关文章里找到儿童python编程入门书籍推、 spring+springmvc+mybatis整合注、 详解vue axios封装请求状态、 等python文章进一步学习,感谢大家的阅读和支持。
展开 +
收起 -
Copyright 2018-2019 xz577.com 码农之家