引入
最近在pycharm中训练模型的时候想画出损失函数曲线,但是发现不太会画,所以想学一下画,粗略学习了一下子,毕竟重点不在画图,而在训练模型。这里绘图是利用了plot折线图,scatter是散点图
主要步骤
实际上就是四大步:创建画布——>准备坐标点的信息——>设置曲线格式——>显示曲线
最基础图绘制
绘制最基础图,感受大概流程,注意绘图有两种方法,一种是直接绘制;一种是取出坐标轴绘制,也即面向这个坐标轴对象绘制,以后我们都用面向坐标轴对象绘制
import matplotlib.pyplot as plt
from matplotlib_inline import backend_inline
# 设置图为向量图显示,这样看上去更高清
backend_inline.set_matplotlib_formats('svg')
# 创建新图窗
Fig1 = plt.figure()
# x,y的数值
# 注意x与y的数据值要一样
x = [1,2,3,4,5]
y = [1,8,27,64,125]
# 画图,画出折线图
plt.plot(x,y)
# 保存图,以svg格式保存,当然.png与.jpg也是可以的
Fig1.savefig("my_img.svg")
# 显示图
plt.show()
'''
# 另外一种画图方法,是一种面向对象的方法
# .axes是坐标轴,相当于把值给坐标轴然后画出来
Fig2 = plt.figure()
ax2 = plt.axes()
ax2.plot(x,y)
'''
绘制多线条(一个图上画多个线)
Fig1 = plt.figure()
ax1 = plt.axes()
x = [1, 2, 3, 4, 5]
y1 = [1, 8, 27, 64, 125]
y2 = [1, 2, 7, 6, 125]
y3 = [1, 3, 5, 64, 25]
ax1.plot(x, y1)
ax1.plot(x, y2)
ax1.plot(x, y3)
plt.show()
设置坐标轴上下限、标题
x = [1, 2, 3, 4, 5]
y = [1, 8, 27, 64, 125]
Fig2,ax2 = plt.subplots()
ax2.plot(x,y)
ax2.set(xlim = (1,5),ylim=(1,125),title="loss_line",xlabel="epoch",ylabel="loss")
图例
也就是标出每条曲线代表什么
Fig2,ax2 = plt.subplots()
x = [1, 2, 3, 4, 5]
y1 = [1, 8, 27, 64, 125]
y2 = [1, 2, 7, 6, 125]
y3 = [1, 3, 5, 64, 25]
ax2.plot(x,y1,label = 'y1')
ax2.plot(x,y2,label = 'y2')
ax2.plot(x,y3,label = 'y3')
ax2.legend()
plt.show()
添加网格
只需要一下这个函数,当然其也有参数颜色粗细等,这个可以具体查阅其函数参数说明
ax2.grid()
动图
利用动图完全可以画出实时更新的损失函数曲线
from numpy import *
import matplotlib.pyplot as plt
from matplotlib import animation
fig,ax=plt.subplots() #相当于fig=plt.figure(),ax=plt.subplot();ax=plt.subplot也可以是ax=fig.add_subplot()
x=arange(0,2*pi,0.01)
line,=ax.plot(x,sin(x))
def update(i):
line.set_ydata(sin(x+i/10))
return line,
def init():
line.set_ydata(sin(x))
return line,
ani=animation.FuncAnimation(fig=fig,func=update,frames=100,init_func=init,interval=20,blit=False)
plt.show()
注:
一、FuncAnimation函数:ani=animation.FuncAnimation(fig=fig,func=update,frames=100,init_func=init,interval=20,blit=False):
1.fig:绘制动画的画布
2.func:动画函数
3.frams:动画长度,一次循环包含的帧数,在函数运行时,其值会传给动画函数update(i)的形参i
4.init_func:动画的起始状态
5.interval:更新频率,interval=20表示每隔20ms从头来一次
6.blit:是否更新整张图,False表示更新整张图,True表示只更新有变化的点。mac用户请使blit=False。
二、ax.plot(x,sin(x))函数返回的line是二维曲线信息,可利用方法点出其坐标信息并修改
完整测试代码
import matplotlib.pyplot as plt
from matplotlib_inline import backend_inline
# 设置图为向量图显示,这样看上去更高清
backend_inline.set_matplotlib_formats('svg')
def fundement():
# 创建新图窗
Fig1 = plt.figure()
# x,y的数值
# 注意x与y的数据值要一样
x = [1,2,3,4,5]
y = [1,8,27,64,125]
# 画图,画出折线图
plt.plot(x,y)
# 保存图,以svg格式保存,当然.png与.jpg也是可以的
Fig1.savefig("my_img.svg")
# 显示图
plt.show()
'''
# 另外一种画图方法,是一种面向对象的方法
# .axes是坐标轴,相当于把值给坐标轴然后画出来
Fig2 = plt.figure()
ax2 = plt.axes()
ax2.plot(x,y)
'''
# 绘制多线条(一个图上画多个线)
def draw_numbers():
Fig1 = plt.figure()
ax1 = plt.axes()
x = [1, 2, 3, 4, 5]
y1 = [1, 8, 27, 64, 125]
y2 = [1, 2, 7, 6, 125]
y3 = [1, 3, 5, 64, 25]
ax1.plot(x, y1)
ax1.plot(x, y2)
ax1.plot(x, y3)
plt.show()
# 绘制多个子图
def draw_subplot():
Fig1 = plt.figure()
x = [1, 2, 3, 4, 5]
y1 = [1, 8, 27, 64, 125]
y2 = [1, 2, 7, 6, 125]
y3 = [1, 3, 5, 64, 25]
# subplot里面的参数意思是(行,列,第几个),也就是所有子图三行一列排布,此子图是第一个子图
plt.subplot(3,1,1)
plt.plot(x,y1)
plt.subplot(3,1,2)
plt.plot(x, y2)
plt.subplot(3,1,3)
plt.plot(x, y3)
plt.show()
'''
# 面向对象方式
Fig2,ax2 = plt.subplots(3)
ax2[0].plot(x,y1)
ax2[1].plot(x,y2)
ax2[2].plot(x,y3)
'''
# 设置坐标轴上下限、标题
def set_lim():
x = [1, 2, 3, 4, 5]
y = [1, 8, 27, 64, 125]
'''
Fig1 = plt.figure()
x = [1, 2, 3, 4, 5]
y = [1, 8, 27, 64, 125]
plt.plot(x,y)
plt.title("loss_line")
plt.xlabel("epoch")
plt.ylabel("loss")
plt.xlim(1,5)
plt.ylim(1,125)
'''
# 以上代码太多,不会用,而面向对象统一了以上设定,结合成了一个
Fig2,ax2 = plt.subplots()
ax2.plot(x,y)
ax2.set(xlim = (1,5),ylim=(1,125),title="loss_line",xlabel="epoch",ylabel="loss")
# 图例,也就是标出每条曲线代表什么
def img_legend():
Fig2,ax2 = plt.subplots()
x = [1, 2, 3, 4, 5]
y1 = [1, 8, 27, 64, 125]
y2 = [1, 2, 7, 6, 125]
y3 = [1, 3, 5, 64, 25]
ax2.plot(x,y1,label = 'y1')
ax2.plot(x,y2,label = 'y2')
ax2.plot(x,y3,label = 'y3')
ax2.legend()
plt.show()
# draw_subplot()
# img_legend()
draw_numbers()