Matplotlib基础用法
1. 引用
import matplotlib.pyplot as plt
2. figure组成
(1)Figure:顶层级,用来容纳所有绘图元素
(2)Axes:matplotlib宇宙的核心,容纳了大量元素用来构造一幅幅子图,一个figure可以由一个或多个子图组成
(3)Axis:axes的下属层级,用于处理所有和坐标轴,网格有关的元素
(4)Tick:axis的下属层级,用来处理所有和刻度有关的元素
3. 两种使用方式
方式一
#显式创建figure和axes,在上面调用绘图方法
#也被称为OO模式(object-oriented style)
x = np.linspace(0, 2, 100)
fig, ax = plt.subplots()
ax.plot(x, x, label='linear')
ax.plot(x, x**2, label='quadratic')
ax.plot(x, x**3, label='cubic')
ax.set_xlabel('x label')
ax.set_ylabel('y label')
ax.set_title("Simple Plot")
ax.legend()
方式二
#依赖pyplot自动创建figure和axes,并绘图
x = np.linspace(0, 2, 100)
plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()