Updating the straight line in a loop in matplotlib

Here is the simple technique to update the straight line equation in a loop, this is very helpful to show the training process of parameters in case of a linear regression model or classification model.

# Use plt ion 
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(211)
for i in range(10):
    # Plot.
    ax1.scatter(xs, ys) 
    # update the figure.
    plt.plot(xs, i * xs, color='red')
    # Draw the line.
    fig.canvas.draw()
    # Clear the current plot.
    ax1.clear()

Advertisements