0

当我从事数据科学任务时,我想更好地绘制数据可视化,所以我遇到了 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,然后使用 from 的j_theta_1T每个传递值t来绘制红点,使用由interactfrom创建的按钮来跟踪曲线ipywidgets

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

4

1 回答 1

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)
)
于 2020-02-24T13:12:30.817 回答