我正在从事数据科学工作,并且我希望更好地绘制数据可视化图,所以我遇到了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
中一个接一个地取值?
这有点棘手,因为您需要输入浮点值,而且还可以用作列表的索引以获取正确的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)
)