matplotlib绘制动态图

def plot1(data_mat, label_mat, omega, omegas):
	#特征矩阵, 标签矩阵, 最优参数, 全部参数
	#定义一张画布
    fig = plt.figure()
    ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
    line, = ax.plot([], [], "g", lw=2)
    label = ax.text([], [], "")

    def init():
        line.set_data([], [])
        x, y, x_, y_ = [], [], [], []
        for i, p in enumerate(data_mat):
            if label_mat[i] > 0:
                x.append(p[0,0])
                y.append(p[0,1])
            else:
                x_.append(p[0,0])
                y_.append(p[0,1])

        plt.plot(x, y, "bo", x_, y_, "rx")
        plt.axis([-1, 10, -10, 10])
        plt.grid(True)
        plt.xlabel("x")
        plt.ylabel("y")
        plt.title("感知机原始形式")
        return line, label

    def animate(i):

        w0 = omegas[i][0, 0]
        w1 = omegas[i][0, 1]
        b = omegas[i][0, 2]

        if w1 == 0: return line, label
        x1 = -1.0
        y1 = -(b + w0 * x1) / w1
        x2 = 10.0
        y2 = -(b + w0 * x2) / w1
        line.set_data([x1, x2], [y1, y2])
        x1 = 0.0
        y1 = -(b + w0 * x1) / w1
        label.set_text(str([w0, w1]) + " " + str(b))
        label.set_position([x1, y1])
        return line, label

    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(omegas),
                                   interval=10, repeat=False, blit=True)
    plt.show()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/cyj5201314/article/details/115273648