如何更有效地使用来自ipywidgets的交互来绘制数据?

问题描述 投票:0回答:1

我正在从事数据科学工作,并且我希望更好地绘制数据可视化图,所以我遇到了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)

这是什么代码结果

enter image description here

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

我应该如何从j_theta_1T中一个接一个地取值?

python-3.x matplotlib interactive ipywidgets python-interactive
1个回答
0
投票

这有点棘手,因为您需要输入浮点值,而且还可以用作列表的索引以获取正确的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)
)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.