【问题标题】:Python Widget - How made this plot?Python Widget - 这个情节是如何制作的?
【发布时间】:2021-10-24 22:45:36
【问题描述】:

我知道如果我想用小部件绘制/打印一些东西,我应该使用以下代码:

interact(function,variable=(1,3))

现在我面临一个问题。是否可以创建一个输入两个数组的小部件?例如,考虑以下情况:

a= np.linspace(1,2,100)
b= np.linspace(3,4,100)

是否可以看到具有两个不同数组的函数的行为(例如,从区间 a 切换到区间 b)? 下面我尝试了一些东西,但它不起作用..

import numpy as np
import matplotlib.pyplot as plt
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
a= np.linspace(1,2,100)
b= np.linspace(3,4,100)
def test(array,constant):
    f = []
    for x in array:
        f.append(x**2+constant*x)
    plt.plot(f)
    plt.show()
    return f
interact(test,array=(a,b),constant=(1,5))

【问题讨论】:

    标签: python arrays numpy matplotlib widget


    【解决方案1】:

    你可以试试这样的:

    import numpy as np
    import matplotlib.pyplot as plt
    from __future__ import print_function
    from ipywidgets import interact, interactive, fixed, interact_manual
    import ipywidgets as widgets
    
    
    xs = {"a": np.linspace(1, 2, 100), "b": np.linspace(3, 4, 100)}
    
    def test(constant, array):
    
        x = xs[array] 
        f = x**2*constant*x
        plt.plot(x, f)
        plt.show()
    
    interact(test, array=xs.keys(), constant=(1,5))
    

    它将创建一个带有滑块的小部件来控制constant 值和一个下拉菜单来选择两个数组之一。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      相关资源
      最近更新 更多