【问题标题】:how to use interact from ipywidgets more effectively to plot the data?如何更有效地使用来自 ipywidgets 的交互来绘制数据?
【发布时间】:2020-06-07 08:15:22
【问题描述】:

当我从事数据科学任务时,我想更好地绘制数据可视化图,所以我遇到了 python 交互。我以前使用过交互,但在这里我卡在下面的代码中。

import matplotlib.pyplot as plt
import numpy as np

def my_plot_2(t):
    j_theta_1T=[5.3148,4.0691,2.9895,2.076099,1.3287,0.74739,0.3321,0.08304,0,0.08304,0.3321,0.7473,1.3287,2.076099,2.9895,4.0691,5.3148]
    X_T=np.linspace(0,2,17)
    plt.figure(num=0, figsize=(6, 4), dpi=80, facecolor='w', edgecolor='k')
    plt.plot(X_T,j_theta_1T,'b',t,j_theta_1T[0],'ro')
    plt.title('hypothesis_fixed_theta_function_of_X',fontsize=12)
    plt.xlabel('theta_1',fontsize=12)
    plt.ylabel('J_theta_1',fontsize=12)
    plt.grid(which='both')
    plt.show()

my_plot_2(0)

这是代码的结果

这里不是my_plot_2(0) 我想使用interact(my_plot_2, t=(0,2,0.125)) 来传递t 的多个值,然后使用j_theta_1T 中的一个值,并使用t 的每个传递值来绘制红点使用interact 创建的按钮从ipywidgets 指向跟踪曲线。

我应该如何从j_theta_1T 中一一获取值?

【问题讨论】:

  • 你能把你的请求简化一点吗?你能举一个简单的例子来说明你希望t的不同输入发生什么吗?通常,输入小部件具有单个值,或者您从预先确定的输入列表中进行选择。您是否需要用户能够为t 输入多个值,或者只是从一个范围中进行选择?
  • @ac24 感谢您的关注。我想通过tinteract 传递这个范围`(0,2,0.125)`,以便为t 创建滑块。现在t 从范围中选择了一个值(通过使用滑块),同时我想从列表中选择值'j_theta_1T' 以在曲线上绘制该点。如上所述t=0j_theta_1T[0]=5.3148 我画了一个红点。希望现在很清楚。

标签: python-3.x matplotlib interactive ipywidgets python-interactive


【解决方案1】:

这有点棘手,因为您需要一个浮点值输入,而且还需要用作列表的索引以获得正确的 y 值。


import matplotlib.pyplot as plt
import numpy as np
import ipywidgets as ipyw

j_theta_1T=[5.3148,4.0691,2.9895,2.076099,1.3287,0.74739,0.3321,0.08304,0,0.08304,0.3321,0.7473,1.3287,2.076099,2.9895,4.0691,5.3148]
X_T=np.linspace(0,2,17)

def my_plot_2(t):

    plt.figure(num=0, figsize=(6, 4), dpi=80, facecolor='w', edgecolor='k')
    plt.plot(X_T,j_theta_1T,'b',
             t/8,j_theta_1T[t],'ro')
    plt.title('hypothesis_fixed_theta_function_of_X',fontsize=12)
    plt.xlabel('theta_1',fontsize=12)
    plt.ylabel('J_theta_1',fontsize=12)
    plt.grid(which='both')
    plt.show()


ipyw.interact(
    my_plot_2,
    t=ipyw.IntSlider(min=0, 
                     max=(len(j_theta_1T)-1), 
                     step=1, 
                     value=0)
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2017-02-28
    • 2012-04-28
    • 1970-01-01
    相关资源
    最近更新 更多