0

As I am working on the data science assignment and I want to plot data visualization better so I come across the python interact. I had used the interact previously but here I am stuck in the following code.

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)

This what the result of the code

enter image description here

Here instead of my_plot_2(0) I want to use interact(my_plot_2, t=(0,2,0.125)) to pass the multiple values for t and then use one by one value from j_theta_1T and use each pass value of t to plot the red dot point to trace the curve using the button created by interact from ipywidgets.

how should I pick up one by one value from j_theta_1T?

anil
  • 37
  • 1
  • 8
  • Can you simplify your request down a bit? Can you give a simple example of what you want to happen to the different inputs of `t`? In general input widgets have a single value, or you choose from a list of predetermined inputs. Do you need the user to be able to input multiple values for `t` or just choose from a range? – ac24 Feb 24 '20 at 10:13
  • @ac24 Thanks for your kind attention. I want to pass this range ` (0,2,0.125)` for `t` through `interact` so that to create the slider for `t`. Now as the `t` chose one value from the range (by using slider ), the same time I want to select the value from the list 'j_theta_1T` to plot that point on the curve. As above for `t=0` and `j_theta_1T[0]=5.3148` i plot one red dot.hope it is clear now. – anil Feb 24 '20 at 12:55

1 Answers1

1

This is a little tricky as you need a float value input, but also to use as an index into a list to get the correct y value.


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)
)
ac24
  • 5,325
  • 1
  • 16
  • 31