python繪圖入門
阿新 • • 發佈:2018-04-16
otl 繪制圖形 for __init__ axis list htm matplot scatter
python繪圖入門
學習了:https://zhuanlan.zhihu.com/p/34200452
API:https://matplotlib.org/api/pyplot_api.html
plot.py:
# 導入模塊 as 取別名 import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams["font.sans-serif"] = ["YouYuan"] # 數據 列表 # X 軸 # input_values = [1,2,3,4,5] input_values = list(range(1024)) # Y 軸 #squares = [1,4,9,16,25] squares = [a ** 2 for a in input_values] # 繪制圖形 # plt.plot(input_values, squares) plt.scatter(input_values, squares,c=squares, cmap=plt.cm.Blues) # 設置標題,坐標軸加上標簽 plt.title("中文Square Number",fontsize=24) # plt.xlabel(‘Value‘, fontsize=14) # plt.ylabel(‘Square of value‘, fontsize=14)# 設置刻度的大小 # plt.tick_params(axis=‘both‘,labelsize=14) # 展示出來 plt.show()
RandomWalk.py:
from random import choice class RandomWalk: def __init__(self, num_points=500): ‘‘‘初始化參數‘‘‘ self.num_points = num_points # 初始位置 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 # 計算下一個點 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)
777.py:
import matplotlib.pyplot as plt from RandomWalk import RandomWalk # 實例化類 rw = RandomWalk(5000) points_numbers = list(range(5000)) rw.fill_walk() # plt.plot(rw.x_values,rw.y_values) plt.scatter(rw.x_values,rw.y_values, s=15,c=points_numbers,cmap=plt.cm.Blues) plt.show()
python繪圖入門