greedy_zero 2022-02-15 20:50 采纳率: 71.4%
浏览 71

如何使用python matplotlib做一个实时刷新的动画

在学习人工智能时,使用matplotlib绘制散点图以及预测函数时希望能够动态的显示每一次预测的线性函数而不是只展现结果

import numpy as np
from matplotlib import pyplot as plt


def get_beans(counts):
    xs = np.random.rand(counts)
    xs = np.sort(xs)
    ys = [1.2 * x + np.random.rand() / 10 for x in xs]
    return xs, ys


xs,ys = get_beans(100)
plt.title("test")
plt.xlabel("Bean size")
plt.ylabel("Toxicity")
plt.scatter(xs,ys)

w=0.5
alpha = 0.05
for i in range(100):
    for i in range(100):
        x = xs[i]
        y = ys[i]
        y_pre = x * w
        w = w + alpha*(y-y_pre)*x
y_pre = xs * w
plt.plot(xs, y_pre)
plt.show()

img

  • 写回答

1条回答 默认 最新

  • smartisong 2022-02-15 22:10
    关注

    你在每一次预测的时候,预测结果都画一下,然后延时一下,这样就可以动态显示预测结果了

    评论

报告相同问题?

问题事件

  • 创建了问题 2月15日