Task01:Matplotlib初相识

一、明晰绘制一张图的组成条件

Figure:最基本的一级
Axes:在Figure上创建子图的容器(如果Figure中仅含一子图,则该容器可省略)
Axis:用于处理子图上和坐标轴和网格相关的元素
Tick:用于处理和刻度相关的元素

二、两种绘图模式模板

A.OO模式:更能实现高级绘图的需求

# step1 准备数据
x = np.linspace(0, 2, 100)
y = x**2

# step2 第五章:设置绘图样式(非必须)
mpl.rc('lines', winewidth=4, linestyle='-.')

# step3 第三章:定义布局
fig, ax = plt.subplots()

# step4 第二章:绘制图像
ax.plot(x, y, label="linear")

# step5 第四章:标签,文字和图例
ax.set_xlabel('x label') 
ax.set_ylabel('y label') 
ax.set_title("Simple Plot")  
ax.legend()
plt.show()

B.pyplot模式:方便简洁,容易上手

# step1 准备数据
x = np.linspace(0, 2, 100)
y = x**2

# step2 第五章:设置绘图样式(非必须)
mpl.rc('lines', winewidth=4, linestyle='-.')

# step3 第二章:绘制图像
plt.plot(x, y, label="linear")

# step4 第五章:标签,文字和图例
plt.xlabel('x label')
plt.ylabel('y label')
plt.title('Simple Plot')
plt.legend()
plt.show()
posted @ 2022-05-15 11:20  Mundane-_-  阅读(46)  评论(0编辑  收藏  举报