欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python从入门到实践15章的几个自己的小程序

发布时间:2024/4/18 python 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python从入门到实践15章的几个自己的小程序 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

练习15-1自己的答案

#加入python画图的库 pyplot import matplotlib.pyplot as plt #生成一个list input_values= list(range(1,6)) #根据代码迭代生成一个列表 cube = [i ** 3 for i in input_values] #调用plt的scatter方法,c代表颜色,s代表每个点的面积 plt.scatter(input_values,cube,c='red',s = 30) #指定x轴的标签和标签大小 plt.xlabel("values",fontsize=15) #指定y轴的标签和标签大小 plt.ylabel("cubic",fontsize=15) #指定图片的标题 plt.title("first program",fontsize=15) #指定坐标轴刻度和刻度大小,tick英文意思是标记、刻度 plt.tick_params(axis='both',labelsize=10) #指定坐标轴的范围 plt.axis([0,50,0,50]) #指定保存的文件名savefig("file_name.png"),其中扩展名自动是png,扩展名可以不指定 plt.savefig('cube.png')

15.3.3绘制随机漫步图

#random_walk.py from random import choice#新建一个类,类的抽象性能更好地解决问题,而且代码更清晰 class RandomWalk():"""一个生成随机漫步数据的类"""def __init__(self,num_points=5000):self.num_points = num_points#所有的随机漫步都从(0,0)开始#这两个列表都存储每一步的横坐标和纵坐标的位置self.x_values = [0]self.y_values = [0]def fill_walk(self):"""计算随机漫步包含的所有点"""while len(self.x_values) < self.num_points:#决定前进方向以及沿这个方向前进的距离#choice方法作用,从一个列表、元组或字符串中随机返回一个值x_direction = choice([1,-1])x_distance = choice([0,1,2,3,4])x_step = x_direction * x_distancey_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的值#x_values[-1]表示列表的最右一个元素,是不是很简单next_x = self.x_values[-1] + x_stepnext_y = self.y_values[-1] + y_step#在2个列表的末尾添加2个新的位置self.x_values.append(next_x)self.y_values.append(next_y) #walk.py from random_walk import RandomWalk import matplotlib.pyplot as plt#rm是类RandomWalk的一个实例 rm = RandomWalk() #运行rm的方法fill_walk,使得这个实例完成随机漫步 rm.fill_walk() #rm完成随机漫步后,rm的属性就改变了,调用2个属性(都是列表)进行画图 #s是面积,我个人的理解是每个点的面积(单位是像素哦) plt.scatter(rm.x_values,rm.y_values,s=10) #显示出图像,也可以使用plt.savefig("figname"),其中文件名的扩展名默认值是png,用户可以省略 #采用编译器给的默认值png plt.show()

效果

练习15.3

import matplotlib.pyplot as plt from randomwalk import RandomWalk while True:rw = RandomWalk()rw.fill_walk()plt.plot(rw.x_values,rw.y_values,linewidth = 4)plt.show()keep_running = input("Another walk(y/n)?")if keep_running == 'n':break import matplotlib.pyplot as plot from random import choice class RandomWalk():def __init__(self,num_points=5000):self.num_points = num_pointsself.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_distancey_direction = choice([-1,1])y_distance = choice([0,1,2,3,4])y_step = y_direction * y_distanceif x_step == 0 and y_step == 0:continuenext_x = self.x_values[-1] + x_stepnext_y = self.y_values[-1] + y_stepself.x_values.append(next_x)self.y_values.append(next_y)

15.5

1.py

import matplotlib.pyplot as plt from randomwalk import RandomWalk while True:rw = RandomWalk()rw.fill_walk()plt.plot(rw.x_values,rw.y_values,linewidth = 4)plt.show()keep_running = input("Another walk(y/n)?")if keep_running == 'n':break

randomwalk.py 

import matplotlib.pyplot as plot from random import choice class RandomWalk():def __init__(self,num_points=5000):self.num_points = num_pointsself.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:continuenext_x = self.x_values[-1] + x_stepnext_y = self.y_values[-1] + y_stepself.x_values.append(next_x)self.y_values.append(next_y)def get_step(self):direction = choice([-1,1])distance = choice([0,1,2,3,4])return direction * distance

总结

以上是生活随笔为你收集整理的python从入门到实践15章的几个自己的小程序的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。