2017-10-08 123 views
1

Jupyter中的以下Python代碼顯示了一個顯示一些生成的散點圖數據的示例,它疊加了一條可以交互式更改y截距和斜率的線,還顯示了均方根錯誤。我的問題是:我怎樣才能讓它更具響應性?有一個滯後和積累的變化得到處理,並且閃爍很多。它可以更快,更敏捷,更順暢嗎?matplotlib平滑動畫疊加在散點圖上

///

%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 

///

enter image description here

回答

0

你需要有plt.show()在你的函數結束。有一個github issue about it

嘗試使用FloatSlidercontinuous_update=FalseSee 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)) 
+0

添加plt.show()仍會顯示積壓的渲染序列。我不確定這是否有一個好的解決方案。但是下面的URL呢? - > https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Asynchronous.html – user2942693

+0

您是否在函數'f'的末尾添加了'plt.show()'?這對我有效。確保您已將所有軟件包升級到最新版本。 –

+0

是的。我把plt.show()作爲函數f中的最後一個語句。我猜它正在按照它應該工作的方式工作。謝謝。 – user2942693