0 引言
Matplotlib 是个很好的绘图包,有时候数据分析时,绘图显示会更简洁明了,机器学习和深度学习完成后也需要绘制训练结果图,总的来说,这个绘图包用起来还是很方便,很有效果的,什么散点图,曲线图,直方图,3D图等。
1 基础用法
绘制一条最基本的直线图
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1,1,100) # 从-1到1生成100个点
y = 2*x +1
plt.plot(x,y)
plt.show()

2 figure 图像
通过 .figure 也能绘制曲线
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1,1,100)
y1 = 2*x +1
y2 = x**2
plt.figure()
plt.plot(x,y1)
plt.figure()
plt.plot(x,y2)
plt.show()


图像大小,也可以在 .figure中的参数figsize设置尺寸大小
x = np.linspace(-1,1,100)
y1 = 2*x +1
y2 = x**2
plt.figure()
plt.plot(x,y1)
plt.figure(figsize=(8,5))
plt.plot(x,y2)
plt.show()


也可以把直线和曲线放在一个图中,.plot 可以自定义线的风格
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')
plt.plot(x,y2,color='blue',linewidth=5.0,linestyle='-')
plt.show()
