隨機漫步這個例子出自《Python編程:從入門到實踐》
創建RandomWalk()類
包含__init__()和fill_walk()
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: #決定前進方向以及沿這個方向前進的距離 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)
繪制隨機漫步圖
rw_visual.py
import matplotlib.pyplot as plt from random_walk import RandomWalk #構建一個RandomWalk實例,並將其包含的點都繪制出來 rw = RandomWalk() rw.fill_walk() plt.scatter(rw.x_values, rw.y_values, s=1) plt.show()

每次都不一樣,如果想找它的先后順序,我們就要給它上色
給點着色
cmap再次出場
關鍵語句
plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor=None, s=15)
隨着c的變化,顏色會漸變,藝術品的感jio
全部代碼
import matplotlib.pyplot as plt from random_walk import RandomWalk while True: #構建一個RandomWalk實例,並將其包含的點都繪制出來 rw = RandomWalk() rw.fill_walk() point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor=None, s=15) plt.show() keep_running = input("Make another walk?(y/n):")#多畫幾次 if keep_running == 'n': break


換色吧,cmap=plt.cm.YlOrBr,應該還有很多好看的哈哈哈

什么?你還想知道起點和終點?
繪制起點和終點
起點(0,0)用綠色,終點用紅色,而且比較大(s=100)
關鍵語句
#突出起點和終點 plt.scatter(0, 0, c='green', edgecolor=None, s=100) plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor=None, s=100)
全部代碼
import matplotlib.pyplot as plt from random_walk import RandomWalk while True: #構建一個RandomWalk實例,並將其包含的點都繪制出來 rw = RandomWalk() rw.fill_walk() point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor=None, s=15) #突出起點和終點 plt.scatter(0, 0, c='green', edgecolor=None, s=100) plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor=None, s=100) plt.show() keep_running = input("Make another walk?(y/n):") if keep_running == 'n': break


一目了然
隱藏坐標軸
關鍵語句
#隱藏坐標軸 plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False)
全部代碼
import matplotlib.pyplot as plt from random_walk import RandomWalk while True: #構建一個RandomWalk實例,並將其包含的點都繪制出來 rw = RandomWalk() rw.fill_walk() point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor=None, s=15) #突出起點和終點 plt.scatter(0, 0, c='green', edgecolor=None, s=100) plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor=None, s=100) #隱藏坐標軸 plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.show() keep_running = input("Make another walk?(y/n):") if keep_running == 'n': break
增加點數
創建實例時,給它一個較大的num_points
關鍵語句
rw = RandomWalk(50000)
全部代碼
import matplotlib.pyplot as plt from random_walk import RandomWalk while True: #構建一個RandomWalk實例,並將其包含的點都繪制出來 rw = RandomWalk(50000) rw.fill_walk() point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor=None, s=1) #突出起點和終點 plt.scatter(0, 0, c='green', edgecolor=None, s=100) plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor=None, s=100) #隱藏坐標軸 plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.show() keep_running = input("Make another walk?(y/n):") if keep_running == 'n': break


調整尺寸以適應屏幕
在fill_walk()后插入關鍵語句
#設置繪圖窗口的尺寸 plt.figure(figsize=(10, 6))
這里第一次提到figure(),用於指定圖表的寬度、高度、分辨率和背景色。你需要給形參figsize指定一個元組,向matplotlib指出繪圖窗口的尺寸,單位為英寸。
Python假定屏幕分辨率為80像素/英寸。如果你知道自己的系統的分辨率,可使用形參dpi向figure()傳遞該分辨率,以有效地利用可用的屏幕空間,如下所示:
plt.figure(dpi=128, figsize=(10, 6))
2020-03-04
