python绘图入门
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()