python从入门到实践15章的几个自己的小程序
生活随笔
收集整理的這篇文章主要介紹了
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':breakrandomwalk.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章的几个自己的小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 离散数学及其应用上的一个问题
- 下一篇: ps学习经历