在活页夹上使用 ipyvidgets 和 matplotlib 进行交互式绘图会生成静态图像
Interactive plot with ipyvidgets and matplotlib on binder produces static images
我正在尝试在 binder 上共享一个 github 存储库。在本地,我使用 matplotlib 和 @interact 的交互式绘图工作正常。在活页夹上,它工作了一半。当滑块值更改时,相同的代码将静态图像添加到活页夹笔记本中的单元格输出。
问题:如何修复活页夹行为并制作交互式绘图?
git 存储库 https://github.com/queezz/Complex_Numbers
我的笔记本是这样的:
%pylab inline
from ipywidgets import interact, widgets
x = np.linspace(0,np.pi,100)
@interact
def plot_interactive(a=widgets.FloatSlider(min=1, max=10, val=1)):
plot(x,np.sin(x*a))
gca().set_aspect('equal')
ylim(-1.1,1.1)
活页夹截图:
找到一个很好的工作示例:
https://github.com/Kapernikov/ipywidgets-tutorial
它的要点是使用%matplotlib widget
和@widgets.interact
。
现在似乎不鼓励使用 %pylab inline
,请参阅 this git issue。
我从教程中复制了一部分代码,它产生的结果与问题中想要的相同。
%matplotlib widget
import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np
# set up plot
fig, ax = plt.subplots(figsize=(6, 4))
ax.set_ylim([-4, 4])
ax.grid(True)
# generate x values
x = np.linspace(0, 2 * np.pi, 300)
def my_sine(x, w, amp, phi):
"""
Return a sine for x with angular frequeny w and amplitude amp.
"""
return amp*np.sin(w * (x-phi))
@widgets.interact(w=(0, 10, 1), amp=(0, 4, .1), phi=(0, 2*np.pi+0.01, 0.01))
def update(w = 1.0, amp=1, phi=0):
"""Remove old lines from plot and plot new one"""
[l.remove() for l in ax.lines]
ax.plot(x, my_sine(x, w, amp, phi), color='C0')
我今天没能得到@queez 的回答;但是,ipywidgets 文档包含 a matplotlib example that I was able to take and adapt @qqueezz's 以使其正常工作。这似乎是制作交互式情节的更简化的途径。
#%matplotlib inline # from the example in the documentation. but doesn't seem necessary in current JupyterLab 3.1.11 or the classic notebook available now https://github.com/fomightez/communication_voila
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
def my_sine(x, w, amp, phi):
"""
Return a sine for x with angular frequency w and amplitude amp.
"""
return amp*np.sin(w * (x-phi))
def f( w, amp, phi):
plt.figure(2)
x = np.linspace(0, 2 * np.pi, 300)
plt.plot(x, my_sine(x, w, amp, phi), color='C0')
#plt.ylim(-5, 5)
plt.grid(True) #optional grid
plt.show()
interactive_plot = interactive(f, w=(0, 10, 1), amp=(0, 4, .1), phi=(0, 2*np.pi+0.01, 0.01))
#output = interactive_plot.children[-1]
#output.layout.height = '450px'
interactive_plot
您可以转到存储库 here,通过选择 'Direct links to start out in notebook mode:' 下 'Start with the matplotlib & widget demo as a notebook' 右侧的 launch binder
标志启动会话。
或者单击 here 通过 MyBinder 直接启动该笔记本。
我正在尝试在 binder 上共享一个 github 存储库。在本地,我使用 matplotlib 和 @interact 的交互式绘图工作正常。在活页夹上,它工作了一半。当滑块值更改时,相同的代码将静态图像添加到活页夹笔记本中的单元格输出。
问题:如何修复活页夹行为并制作交互式绘图?
git 存储库 https://github.com/queezz/Complex_Numbers
我的笔记本是这样的:
%pylab inline
from ipywidgets import interact, widgets
x = np.linspace(0,np.pi,100)
@interact
def plot_interactive(a=widgets.FloatSlider(min=1, max=10, val=1)):
plot(x,np.sin(x*a))
gca().set_aspect('equal')
ylim(-1.1,1.1)
活页夹截图:
找到一个很好的工作示例: https://github.com/Kapernikov/ipywidgets-tutorial
它的要点是使用%matplotlib widget
和@widgets.interact
。
现在似乎不鼓励使用 %pylab inline
,请参阅 this git issue。
我从教程中复制了一部分代码,它产生的结果与问题中想要的相同。
%matplotlib widget
import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np
# set up plot
fig, ax = plt.subplots(figsize=(6, 4))
ax.set_ylim([-4, 4])
ax.grid(True)
# generate x values
x = np.linspace(0, 2 * np.pi, 300)
def my_sine(x, w, amp, phi):
"""
Return a sine for x with angular frequeny w and amplitude amp.
"""
return amp*np.sin(w * (x-phi))
@widgets.interact(w=(0, 10, 1), amp=(0, 4, .1), phi=(0, 2*np.pi+0.01, 0.01))
def update(w = 1.0, amp=1, phi=0):
"""Remove old lines from plot and plot new one"""
[l.remove() for l in ax.lines]
ax.plot(x, my_sine(x, w, amp, phi), color='C0')
我今天没能得到@queez 的回答;但是,ipywidgets 文档包含 a matplotlib example that I was able to take and adapt @qqueezz's
#%matplotlib inline # from the example in the documentation. but doesn't seem necessary in current JupyterLab 3.1.11 or the classic notebook available now https://github.com/fomightez/communication_voila
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
def my_sine(x, w, amp, phi):
"""
Return a sine for x with angular frequency w and amplitude amp.
"""
return amp*np.sin(w * (x-phi))
def f( w, amp, phi):
plt.figure(2)
x = np.linspace(0, 2 * np.pi, 300)
plt.plot(x, my_sine(x, w, amp, phi), color='C0')
#plt.ylim(-5, 5)
plt.grid(True) #optional grid
plt.show()
interactive_plot = interactive(f, w=(0, 10, 1), amp=(0, 4, .1), phi=(0, 2*np.pi+0.01, 0.01))
#output = interactive_plot.children[-1]
#output.layout.height = '450px'
interactive_plot
您可以转到存储库 here,通过选择 'Direct links to start out in notebook mode:' 下 'Start with the matplotlib & widget demo as a notebook' 右侧的 launch binder
标志启动会话。
或者单击 here 通过 MyBinder 直接启动该笔记本。