推荐使用哪种绘图方式?matplotlib 还是 pylab?

matplotlibserver side programmingprogramming更新于 2025/10/9 22:52:17

pylab 是一个模块,它在同一个命名空间中导入了 matplotlib.pyplot(用于绘图)和 numpy(用于数学运算和数组处理)。

虽然很多示例都使用了 pylab,但现在已经不再推荐了。对于非交互式绘图,建议使用 pyplot 创建图形,然后使用面向对象接口进行绘图。

示例

import matplotlib.pyplot as plt
import numpy as np

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()
plt.show()

输出


相关文章