import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots() # 创建一个包含一个axes的figure
ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # 绘制图像
还可以通过一种更简单的方式绘制图像,matplotlib.pyplot方法能够直接在当前axes上绘制图像,如果用户未指定axes,matplotlib会帮你自动创建一个。所以上面的例子也可以简化为以下这一行代码。
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
绘图接口
matplotlib提供了两种最常用的绘图接口
1)显式创建figure和axes,在上面调用绘图方法,也被称为OO模式(object-oriented style)
2)依赖pyplot自动创建figure和axes,并绘图
使用第一种绘图接口,是这样的:
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()
而如果采用第二种绘图接口,绘制同样的图,代码是这样的:
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()