《Python编程:从入门到实践》---项目2

第15章 生成数据

15-1 立方:

import matplotlib.pyplot as plt

'''
x_values = [1, 2, 3, 4, 5]  
y_values = [x**3 for x in x_values]

plt.scatter(x_values,y_values,s=100)
#plt.plot(x_values,y_values, linewidth=5)# 设置图表标题,并给坐标轴加上标签 
plt.title("Cubic Numbers", fontsize=24)  
plt.xlabel("Value", fontsize=14)
plt.ylabel("Cubic of Value", fontsize=14)# 设置刻度标记的大小 

plt.tick_params(axis='both', which='major',labelsize=14)

plt.show()

'''

x_values = list(range(1,5001))
y_values = [x**3 for x in x_values]

plt.scatter(x_values,y_values,s=100)
#plt.plot(x_values,y_values, linewidth=5)# 设置图表标题,并给坐标轴加上标签 
plt.title("Cubic Numbers", fontsize=24)  
plt.xlabel("Value", fontsize=14)
plt.ylabel("Cubic of Value", fontsize=14)# 设置刻度标记的大小 

plt.tick_params(axis='both', which='major',labelsize=14)

plt.show()

15-2 彩色立方

plt.scatter(x_values,y_values,c='red',cmap=plt.cm.Blues,
		edgecolor='none',s=10)

15-3 分子运动 + 15-4 改进的随机漫步

random_walk.py

from random import choice
class RandomWalk():
	def __init__(self, num_points=500):
		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,5,6,7,8])
			x_step = x_direction * x_distance
		
			y_direction = choice([1, -1])
			y_distance = choice([0, 1, 2, 3, 4,5,6,7,8])
			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 random_walk import RandomWalk

rw = RandomWalk()
rw.fill_walk()

#plt.scatter(rw.x_values, rw.y_values, s=15)
plt.plot(rw.x_values, rw.y_values, linewidth=1)
plt.show()

(1),将x,y的方向删除 (只保留1,或 -1)

《Python编程:从入门到实践》---项目2

(2,),原始状态

《Python编程:从入门到实践》---项目2

15-5 重构

random_walk.py

关于类的函数,还要复习

from random import choice
class RandomWalk():
	def __init__(self, num_points=500):
		self.num_points = num_points
		self.x_values = [0]
		self.y_values = [0]

	def get_step(self):
		direction = choice([1,-1])
		distance = choice([0, 1, 2, 3, 4,5,6,7,8])
		step = direction * distance
		
		return step
	
	def fill_walk(self):
		while len(self.x_values) < self.num_points:
			x_step=self.get_step()#调用类中的函数,要用self.+函数名的形式
			y_step=self.get_step()
			
			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)

die.py:

from random import randint

class Die():
	"""表示一个骰子的类""" 
	def __init__(self, num_sides=6):
		"""骰子默认为6面"""
		self.num_sides = num_sides
	def roll(self):
		""""返回一个位于1和骰子面数之间的随机值""" 
		return randint(1, self.num_sides)

15-6 自动生成标签:

from die import Die
import pygal
# 创建一个D6 
die_1 = Die()
die_2 = Die(10)
# 掷几次骰子,并将结果存储在一个列表中
results = [] 
for roll_num in range(100):
	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 one D6 1000 times." 

#答案
x_values=list(range(1,20))
hist.x_labels = [x for x in x_values]

hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D6', frequencies)
hist.render_to_file('dice_visual.svg')

15-7 两个D8骰子:

以10的次方为量级:

掷骰子次数最大可达:100000次

15-8 同时掷三个骰子:

from die import Die
import pygal
# 创建一个D6 
die_1 = Die(8)
die_2 = Die(8)
die_3 = Die(8)
# 掷几次骰子,并将结果存储在一个列表中
results = [] 
for roll_num in range(1000):
	result = die_1.roll()+die_2.roll()+die_3.roll()
	results.append(result)
frequencies=[]
max_result=die_1.num_sides+die_2.num_sides+die_3.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 one D6 1000 times." 
x_values=list(range(3,25))
hist.x_labels = [x for x in x_values]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add('D6', frequencies)
hist.render_to_file('dice_visual.svg')

15-9 略

15-10 练习使用本章介绍的两个库