简体   繁体   中英

matplotlib smooth animation superimposed on scatter plot

The following Python code in Jupyter shows an example that displays some generated scatter plot data, and it superimposes a line that you can interactively change the y intercept and slope, It also displays the root mean square error. My question is this: How can I make it more responsive? there is a lag and an accumulation of changes that get processed, and it flickers a lot. Can it be faster and more responsive and smoother?

///

%matplotlib inline

from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np

# Desired mean values of generated sample.
N = 50

# Desired mean values of generated sample.
mean = np.array([0, 0])

# Desired covariance matrix of generated sample.
cov = np.array([
        [ 10,  8],
        [  8, 10]
    ])

# Generate random data.
data = np.random.multivariate_normal(mean, cov, size=N)
xdata = data[:, 0]
ydata = data[:, 1]

# Plot linear regression line
def f(m, b):
    plt.figure()
    x = np.linspace(-10, 10, num=100)
    plt.plot(xdata, ydata, 'ro')
    plt.plot(x, m * x + b)
    plt.ylim(-10, 10)
    rmes = np.sqrt(np.mean(((xdata*m+b)-ydata)**2))
    print("Root Mean Square Error: ", rmes)

interactive_plot = interactive(f, m=(-10.0, 10.0), b=(-10, 10, 0.5))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot

///

在此处输入图片说明

You need to have plt.show() at the end of your function. There was a github issue about it

Try using a FloatSlider with continuous_update=False . See here

interactive_plot = interactive(f, m=FloatSlider(min=-10.0, max=10.0,  continuous_update=False), 
                               b=FloatSlider(min=-10, max=10, step=.5, continuous_update=False))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM