[[toc]]
matplotlib 是 Python 主要的科学绘图库,其功能为生成可发布的可视化内容,如折线图、直方图、散点图等。将数据及各种分析可视化,可以让你产生深刻的理解,而我们将用 matplotlib 完成所有的可视化内容。
# 安装与引用
h# 绘制正弦函数
n | import numpy as np |
| import matplotlib.pyplot as plt |
| |
| |
| x = np.arange(0, 6, 0.1) |
| y1 = np.sin(x) |
| y2 = np.cos(x) |
| |
| |
| plt.plot(x, y1, label="sin") |
| plt.plot(x, y2, linestyle="--", label="cos") |
| plt.xlabel('x') |
| plt.ylabel('y') |
| plt.title('sin & cos') |
| plt.legend() |
| plt.show() |
# 绘制阶跃函数
n | import numpy as np |
| import matplotlib.pylab as plt |
| |
| step_function = lambda x: np.array(x > 0, dtype = np.int) |
| |
| x = np.arange(-5.0, 5.0, 0.1) |
| y = step_function(x) |
| |
| plt.plot(x, y) |
| plt.ylim(-0.1, 1.1) |
| plt.show() |
# 绘制图片
n | import matplotlib as plt |
| from matplotlib.image import imread |
| |
| |
| img = imread('your/path/pic.png') |
| |
| plt.imshow(img) |
| plt.show() |