15-1 & 15-2
#15-1立方
"""前5个整数"""
import matplotlib.pyplot as plt
#折线
plt.title("pic", fontsize = 20)
plt.xlabel("x", fontsize = 15)
plt.ylabel("y", fontsize = 15)
x_value = [1,2,3,4,5]
y_value = [1,8,27,64,125]
plt.plot(x_value, y_value, linewidth = 5)
plt.show()
#散点
plt.title("pic", fontsize = 20)
plt.xlabel("x", fontsize = 15)
plt.ylabel("y", fontsize = 15)
x_value1 = [1,2,3,4,5]
y_value1 = [1,8,27,64,125]
plt.scatter(x_value1, y_value1, s = 20)
plt.show()
"""前5000个整数"""
import matplotlib.pyplot as plt
x_values = list(range(1,5001))
y_values = [x**3 for x in x_values]
plt.scatter(x_values, y_values, s=20)
plt.title("pic", fontsize = 20)
plt.xlabel("x", fontsize = 15)
plt.ylabel("y", fontsize = 15)
plt.axis([0,5100,0,125000000100])
plt.show()
#15-2彩色立方
import matplotlib.pyplot as plt
x_values = list(range(1,5001))
y_values = [x**3 for x in x_values]
plt.scatter(x_values, y_values, c=y_values, cmap = plt.cm.Blues, edgecolor = 'none', s=20)
plt.title("pic", fontsize = 20)
plt.xlabel("x", fontsize = 15)
plt.ylabel("y", fontsize = 15)
plt.axis([0,5100,0,125000000100])
plt.show()
![](https://img-blog.csdnimg.cn/2020021221315084.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/2020021221323277.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20200212213743681.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20200212213821744.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)
15-3 分子运动
random_walk.py
from random import choice
class RandomWalk():
"""一个生成随机漫步的类"""
def __init__(self,num = 5000):
"""初始化随机漫步属性"""
self.num = num
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
"""计算随机漫步包含的所有点"""
while len(self.x_values)<self.num:
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
#计算下一个点的x和y值
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)
15-3.py
import matplotlib.pyplot as plt
from random_walk import RandomWalk
rw = RandomWalk(5000)
rw.fill_walk()
point_numbers = list(range(rw.num))
plt.plot(rw.x_values,rw.y_values, linewidth=3)
plt.show()
img
![](https://img-blog.csdnimg.cn/20200212214934153.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)
15-4 改进的随机漫步
删除y_direction中-1的值,y方向只能从下至上运动,将x一次最大移动距离设置为8。
random_walk1.py
from random import choice
class RandomWalk():
"""一个生成随机漫步的类"""
def __init__(self,num = 5000):
"""初始化随机漫步属性"""
self.num = num
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
"""计算随机漫步包含的所有点"""
while len(self.x_values)<self.num:
x_direction = choice([-1,1])
x_distance = choice([0,1,2,3,4,5,6,7,8])
x_step = x_direction * x_distance
y_distance = choice([0,1,2,3,4,5,6,7,8])
y_step = y_distance
if x_step == 0 and y_step == 0:
continue
#计算下一个点的x和y值
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)
15-4.py
import matplotlib.pyplot as plt
from random_walk1 import RandomWalk
while True:
rw = RandomWalk()
rw.fill_walk()
point_numbers = list(range(rw.num))
plt.scatter(rw.x_values,rw.y_values, c = point_numbers,
cmap = plt.cm.Blues,edgecolor = 'none',s=15)
plt.show()
img
![](https://img-blog.csdnimg.cn/20200212215456765.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)
15-5 重构
random_walk2.py
from random import choice
class RandomWalk():
"""一个生成随机漫步的类"""
def __init__(self,num = 5000):
"""初始化随机漫步属性"""
self.num = num
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
while len(self.x_values)<self.num:
x_step = self.get_step()
y_step = self.get_step()
if x_step == 0 and y_step == 0:
continue
#计算下一个点的x和y值
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)
def get_step(self):
directions = choice([-1,1])
distances = choice([0,1,2,3,4,5,6,7,8])
step = directions * distances
return step
15-6 自动生成标签
die.py
from random import randint
class Die():
def __init__(self,num_sides=6):
self.num_sides = num_sides
def roll(self):
return randint(1, self.num_sides)
dice_visual.py
import pygal
from die import Die
die1 = Die()
die2 = Die()
results = [die1.roll()+die2.roll() for roll_num in range(50000)]
frequencies = [results.count(value) for value in range(2,die1.num_sides
+die2.num_sides+1)]
hist = pygal.Bar()
hist.title = "Results of rolling two D6 dice 50000 times."
hist.x_labels = [str(value) for value in range(2,die1.num_sides+
die2.num_sides+1)]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D6+D6', frequencies)
hist.render_to_file('die_visual.svg')
img (.svg文件用浏览器打开)
![](https://img-blog.csdnimg.cn/2020021222042352.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)
15-7 两个D8骰子
die.py 同上
dice_visual.py
import pygal
from die import Die
die1 = Die(8)
die2 = Die(8)
results = [die1.roll()+die2.roll() for roll_num in range(1000000)]
frequencies = [results.count(value) for value in range(2,die1.num_sides
+die2.num_sides+1)]
hist = pygal.Bar()
hist.title = "Results of rolling two D8 dice 1000000 times."
hist.x_labels = [str(value) for value in range(2,die1.num_sides+
die2.num_sides+1)]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D8+D8', frequencies)
hist.render_to_file('die_visual.svg')
img
![](https://img-blog.csdnimg.cn/20200212220922654.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)
15-8 同时掷三个骰子
die.py 同上
dice_visual.py
import pygal
from die import Die
die1 = Die()
die2 = Die()
die3 = Die()
results = [die1.roll()+die2.roll()+die3.roll() for roll_num in range(50000)]
frequencies = [results.count(value) for value in range(3,die1.num_sides
+die2.num_sides+die3.num_sides+1)]
hist = pygal.Bar()
hist.title = "Results of rolling three D6 dice 50000 times."
hist.x_labels = [str(value) for value in range(3,die1.num_sides+
die2.num_sides+die3.num_sides+1)]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('3*D6', frequencies)
hist.render_to_file('die_visual.svg')
img
![](https://img-blog.csdnimg.cn/20200212221235383.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)
15-9 将点数相乘
die.py 同上
dice_visual.py
import pygal
from die import Die
die1 = Die()
die2 = Die()
results = [die1.roll()*die2.roll() for roll_num in range(10000000)]
frequencies = [results.count(value) for value in range(2,die1.num_sides
*die2.num_sides+1)]
hist = pygal.Bar()
hist.title = "Results of rolling two D6 dice 10000000 times."
hist.x_labels = [str(value) for value in range(2,die1.num_sides*
die2.num_sides+1)]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D6*D6', frequencies)
hist.render_to_file('die_visual.svg')
img
![](https://img-blog.csdnimg.cn/20200212221441494.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RvY3Rvcnhj,size_16,color_FFFFFF,t_70)