日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/4/18 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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章的几个自己的小程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。