【发布时间】:2017-10-08 23:42:39
【问题描述】:
Jupyter 中的以下 Python 代码显示了一个示例,该示例显示了一些生成的散点图数据,它叠加了一条线,您可以交互地更改 y 截距和斜率,它还显示均方根误差。我的问题是:我怎样才能使它更具响应性?处理过程中存在滞后和累积变化,并且闪烁很多。能否更快、更灵敏、更流畅?
///
%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
///
【问题讨论】:
标签: python animation matplotlib plot interactive