【问题标题】:widgets not getting displayed in ipywidgets小部件未在 ipywidgets 中显示
【发布时间】:2021-04-26 22:36:56
【问题描述】:

我是 jupyter notebooks 和 python 的初学者。我尝试了以下方法(如 youtube 视频中所建议的那样——我唯一的老师)。

import ipywidgets as widgets
from IPython.display import display
import matplotlib.pyplot as plt
import numpy as np
%matplotlib nbagg 
x = np.linspace(0,2,1000)
fig,ax = plt.subplots(1,figsize =(10,4))

amp=widgets.FloatSlider(min=1,max=10,value=1,description='Amp:')
display(amp)
plt.suptitle('sine wave')
fig.show()

正在显示绘图(当然没有任何曲线 - 我没有创建它们),但小部件没有显示。欢迎在这方面提供任何帮助。

【问题讨论】:

标签: python jupyter-notebook ipywidgets


【解决方案1】:

我认为在您的情况下不显示正弦波是正常的:您只使用plt.subplots 创建一个空的Figure 和一个Axes,并且您显示的滑块与任何滑块都没有任何连接绘图功能!

我建议使用以下代码来绘制具有不同交互属性的正弦波:

%matplotlib widget
import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np

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, 100)
 
 
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')

魔术命令%matplotlib widget需要你安装ipympl:conda install -c conda-forge ipympl。

我从一个非常好的tutorial 中获取了这段代码。

此代码仅适用于单个 Jupyter Notebook;为了在 Jupyter Lab 中使用它,需要安装一个扩展:jupyter labextension install jupyter-matplotlib。

我目前也在学习如何使用 Jupyter 小部件来创建交互式笔记本!

【讨论】:

  • 谢谢恩里科。我尝试使用您提供的代码......但我无法让小部件出现。
  • 什么是单 Jupyter Notebooks ? Jupyter 实验室?
  • @Murali 我不知道你是否知道,但你可以从两个不同的界面使用 Jupyter Notebooks。较旧的是jupyter notebook,较新的是jupyter lab,有时它们的工作方式不同,尤其是小部件。我建议你学习 Jupyter Lab,因为它将成为标准。如果此答案对您不起作用,您可能应该安装一些其他依赖项。检查ipyml GitHub page,尤其是“在 JupyterLab 中使用”部分。
  • 谢谢。我尝试了所有方法...不同的教程...重置内核等,我根本无法获得小部件的显示。这对我的项目非常重要。
  • 如何解决这个问题?我看到很多关于这个主题的讨论,但没有针对这个问题的分步诊断。我可以安装任何其他模块来获取小部件并根据小部件中的选择更新我的绘图...我可以离开 ipywidgets。
【解决方案2】:

如果你想尝试 Plotly 而不是 Matplotlib,请改用以下代码:

import ipywidgets as widgets
from IPython.display import display
import plotly.graph_objs as go
import plotly.offline as py
import plotly.express as px
import numpy as np


# generate x values
x = np.linspace(0, 2 * np.pi, 100)
 
 
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 view_image(w = 1.0, amp=1, phi=0):
    
    y = my_sine(x, w, amp, phi)
    
    fig = px.line(x=x, y=y)
    
    #Fix Y-axis range, otherwise the sine will appear to remain fixed, and only Y-axis values will change.
    fig.update_yaxes(range=(-4, 4))
    #Disable autosizing fo Figure canvas. Otherwise, every time a slider is moved, the canvas size will automatically change.
    fig.update_layout(autosize=False)
    
    fig.show()

要安装 Plotly 并在您的笔记本中使用它,请阅读 this 指南。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 2018-03-14
    • 1970-01-01
    • 2021-05-29
    • 2022-01-02
    • 2019-03-29
    • 1970-01-01
    相关资源
    最近更新 更多