python - Jupyter : How can I interactively select series to plot using widgets. SelectMultiple()?

标签 python jupyter-notebook

背景:

有人问过类似的问题here ,但不是很具体,并且大多仅通过引用其他来源来回答。我的案例感觉很基本,我很惊讶找到这样的工作示例有多么困难。

目标:

我只是希望能够从 pandas 数据框中选择任何子集,通过使用像这样的小部件来制作如下图:

enter image description here

我的尝试:

widgets.SelectMultiple() 小部件在 docs 中有简要描述。 , 和 this section描述了如何以交互方式更改图中系列的值。我试图用 widgets.SelectMultiple() 的功能替换后一个演示的核心部分,但收效甚微。

认为我真的很接近这个工作了,我希望我所要做的就是找出在标记为'# what to do!?' 在下面的代码片段中。正如现在的代码片段所示,生成了一个小部件和图表,但它们之间没有有效的链接。

我知道的问题:

我对链接中提供的示例的复制存在一些缺陷。我认为 dfwidg 应该包含在 multiplot 函数中。 interactive plot 功能可能也是如此。我也尝试过不同的变体,但没有成功。

片段(在 Jupyter Notebook 中使用):

# imports
%matplotlib inline

from ipywidgets import interactive
import pandas as pd
import numpy as np
from jupyterthemes import jtplot

# Sample data
np.random.seed(123)
rows = 50
dfx = pd.DataFrame(np.random.randint(90,110,size=(rows, 1)), columns=['Variable X'])
dfy = pd.DataFrame(np.random.randint(25,68,size=(rows, 1)), columns=['Variable Y'])
dfz = pd.DataFrame(np.random.randint(60,70,size=(rows, 1)), columns=['Variable Z'])

df = pd.concat([dfx,dfy,dfz], axis = 1)
#jtplot.style()

import ipywidgets as widgets
from IPython.display import display

def multiplot():
    opts = df.columns.values

    widg = widgets.SelectMultiple(
           options=opts,
           value=[opts[1]],
           rows=len(opts),
           description='Variables',
           disabled=False)

    display(widg)

    # what to do!? 
    df.plot()

    #attempts:
    #df[widg].plot()
    #df[widg.value[0]].plot()

interactive_plot = interactive(multiplot)
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot

输出(有缺陷):

enter image description here

最佳答案

感谢您提供清晰的示例。不幸的是,我不确定 SelectMultiple 是否可以按照您想要的方式使用。

通常,对于交互调用,您需要一个函数来传递参数。您不需要在函数体内创建小部件,interact 调用应该从传递的参数中了解需要什么类型的输入小部件。

请参阅此处的一些示例,其中您指定了字符串选项列表 (https://ipywidgets.readthedocs.io/en/stable/examples/Using%20Interact.html#Widget-abbreviations)

我对您的代码做了一些小改动,以生成与下拉选择器的交互。我怀疑如果您想使用 SelectMultiple 而不是 Dropdown,这超出了 interact 功能。您可能需要单独创建小部件,然后使用 observe

# imports
%matplotlib inline

from ipywidgets import interactive
import pandas as pd
import numpy as np
# from jupyterthemes import jtplot

# Sample data
np.random.seed(123)
rows = 50
dfx = pd.DataFrame(np.random.randint(90,110,size=(rows, 1)), columns=['Variable X'])
dfy = pd.DataFrame(np.random.randint(25,68,size=(rows, 1)), columns=['Variable Y'])
dfz = pd.DataFrame(np.random.randint(60,70,size=(rows, 1)), columns=['Variable Z'])

df = pd.concat([dfx,dfy,dfz], axis = 1)
#jtplot.style()

import ipywidgets as widgets
from IPython.display import display

def multiplot(a):
    opts = df.columns.values
    df.loc[:, a].plot()

interactive_plot = interactive(multiplot, a=['Variable X', 'Variable Y', 'Variable Z'])
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot

enter image description here

这是一个使用observe、SelectMultiple 小部件和Output 小部件的版本:

# imports
%matplotlib inline

from ipywidgets import interactive
import pandas as pd
import numpy as np
from IPython.display import clear_output
import matplotlib.pyplot as plt

# Sample data
np.random.seed(123)
rows = 50
dfx = pd.DataFrame(np.random.randint(90,110,size=(rows, 1)), columns=['Variable X'])
dfy = pd.DataFrame(np.random.randint(25,68,size=(rows, 1)), columns=['Variable Y'])
dfz = pd.DataFrame(np.random.randint(60,70,size=(rows, 1)), columns=['Variable Z'])

df = pd.concat([dfx,dfy,dfz], axis = 1)
#jtplot.style()

import ipywidgets as widgets
from IPython.display import display

opts = df.columns.values

selector = widgets.SelectMultiple(
options=opts,
value=[opts[1]],
rows=len(opts),
description='Variables',
disabled=False)

output = widgets.Output()

display(selector)
display(output)

def multiplot(widg):
    choices = widg['new']
    data = df.loc[:, choices] if choices else df
    output.clear_output(wait=True)
    with output:
        ax = data.plot()
        plt.show()

selector.observe(multiplot, names='value')

enter image description here

关于python - Jupyter : How can I interactively select series to plot using widgets. SelectMultiple()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629077/

相关文章:

Python Scipy Anneal 寻找超出范围的解决方案

python - 无法在 Mac 上安装 Matplotlib

ssh - 如何远程启动IPython笔记本?

python - 在 jupyter 中混合 python 输出和 Markdown

python - jupyter 笔记本 vs jupyter 控制台 : display of markdown (and latex, html 等)对象

python - Pandas 从对象到 bool 值的转换总是使用 astype 返回 True

python - 从类变量元素构造实例变量 pyparsing 匹配器

python - PySpark:如何在不达到速率限制的情况下调用 API/Web 服务?

python - 如何同时捕获和显示 ipython (jupyter notebook) 单元格输出?

jupyter-notebook - 如何在 google Colab 上关闭和停止 jupyter notebook