0

I'm new to this asking question business so take it easy on me eh?

My goal is to plot each step of a for loop where I have nested an if else statement. I'm not sure where to begin in regards to how to plot each iteration? Any thoughts are greatly appreciated.

Code is here below....

for i in range(1,1001):
    #compute a random number between 0 and 1, following uniform distribution
    x=np.random.uniform(0,1,None)

    if state==0: 
        #we are in state 0
        if x<pi11:
            state=0
            freq_state0=freq_state0+1

        else:
            state=1
            freq_state1=freq_state1+1

    else: #we are in state 1
        if x<(1-pi22):
            state=0
            freq_state0=freq_state0+1

        else:
            state=1
            freq_state1=freq_state1+1
DavidG
  • 24,279
  • 14
  • 89
  • 82
Fruh
  • 1
  • 2
    What kind of plot exactly? Have you looked at `matplotlib`? – Chris_Rands Feb 07 '18 at 09:30
  • 1
    where is `state` initialised? What is `pi11`? Which variable do you want to plot? – Ma0 Feb 07 '18 at 09:32
  • Hey folks! Thanks for the responses. I'm trying to plot as follows.... I'd like the x-axis to be the number of times I run the for loop (so 1 to 1000) and the y-axis to show which 'state' I am in (so i'll be in either 0 or 1). Yes seems like I need to use matplotlib. State and pi11 are both initialized 'above' this part of me code as user input. – Fruh Feb 07 '18 at 19:34

1 Answers1

0

EDIT: Working code

Here's one question, and one answer that might help.

Use matplotlib

Here's a function you can use

    import matplotlib.pyplot as plt

    def plot_chart( iteration_number, freq_state):
        plt.scatter( iteration_number, freq_state)
        plt.show()
        plt.pause(0.0001)
Varun Vembar
  • 318
  • 5
  • 18
  • Thanks for the function! I'm such a novice, I'm not sure how to use the function within my code though? As a reminder, I'm hoping to plot a point for each 'step'. What is your recommendation on where within my code you would place the function? And also how do you get each step of the loop called into the function (e.g. I would like the function to be used 1000 times to plot each time through the for loop. – Fruh Feb 07 '18 at 18:52
  • what values do pi11 and pi22 take? – Varun Vembar Feb 08 '18 at 04:55
  • Added a link to working code, not sure if it'll do what you want, since I don't know what values pi11 and pi22 will take. As to where to call the function, just call it in the loop, after the else-if ladder, but not _inside_ it – Varun Vembar Feb 09 '18 at 04:23
  • hey! sorry for the slow reply. I took another stab at this with your example code (thank you so much!), but am still getting errors. I'll paste my program in another comment, but in short I tried adding what you shared right after the else-if but I get this error. I am using Sypder to run this. File "/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 251, in show return _show(*args, **kw) TypeError: show() got an unexpected keyword argument 'block' – Fruh Feb 18 '18 at 17:42