《python编程从入门到实践》第15章 生成数据 - 动手试一试

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()
图1 5个点立方折线图

 

图2 五个点立方散点图

 

图3 5000个点立方散点图

 

图4 5000个点立方散点+彩色

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

图5 5000个点分子运动图

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 

图6 5000个点从下之上随机漫步图

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文件用浏览器打开)

图7 两个六面骰子共掷50000次频率直方图

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

图8 两个8面骰子共掷1000000次频率直方图

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

图9 三个6面骰子同时掷50000次频率直方图

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

图10 两个6面骰子点数相乘频率直方图

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值