0
import random
import pylab
import matplotlib.pyplot as plt

for count in range(0,1000):

#for loop used in conjunction with a count and range to produce 1000 numbers #

    x=random.gauss(550,30)

    a=pylab.array([50000 ,30000 ,10000 ,0], float)

    s=pylab.array([0 ,100 ,30], float)

    A=pylab.array([((x+(s[0]))/2) ,((x+(s[1]))/2) ,((x+(s[2]))/2)])

    D=pylab.array([(a[0]-a[1]) ,(a[1]-a[2]) ,(a[2]-a[3])])

    T=pylab.array([(D[0]/A[0]) ,(D[1]/A[1]) ,(D[2]/A[2])])

    Tc=pylab.array([(T[0]/20000) ,(T[1]/20000) ,(T[2]/10000)])

    B=pylab.array([(50000) ,(30000) ,(20000) ,(3000) ,(0)], float)

    T3=pylab.array([(Tc[0]*20000) ,(Tc[1]*10000) ,((Tc[1]*10000)+(Tc[2]*7000)),(Tc[2]*3000)])

    S=pylab.array([(2) ,(6) ,(3) ,(0)])

    W=pylab.array([(T3[0]*S[3]) ,(T3[1]*S[2]) ,(T3[2]*S[1]) ,(T3[3]*S[0])])

    W=(4600)+W[0]+W[1]+W[2]+W[3]

    #need to plot W which is generated using the equations stored in the arrays above    based on  #the random values for generated for x.


plt.hist([x], bins=[20])
plt.show()

when I attempt to plot it though the graph produced is always blank ie, nothing is plotted

any help would be greatly appreciated, thanks.

Greg
  • 11,654
  • 3
  • 44
  • 50
user3168461
  • 1
  • 1
  • 1
  • You could use the code in this [answer](http://stackoverflow.com/a/37559471/2087463) or this [answer](http://stackoverflow.com/a/37548733/2087463) as an example for plotting histograms after the data has been created in a loop. – tmthydvnprt Jun 01 '16 at 12:48

1 Answers1

0

If I understand correctly you want to plot generated W values in a histogram. To do this you need to save them in a list, the simplest way to do this from your current script is to add a line prior to the for loop:

W_list = []

This creates an empty array, then after calculating the W value we add

W=(4600)+W[0]+W[1]+W[2]+W[3]
W_list.append(W)

Once the loop has run you have a list of values which can be plotted by

plt.hist(W_list, bins=20)

The reason your script was not running was firstly the value you are trying to plot is [x], this is simply a list of the last random number you generated in the for loop. Secondly by making the number of bins a list bins=[20] you are making matplotlib behave strangely, this should be just a single integer like bins=20.

You should also take care with how you name the variables as this may cause bugs in future. For example you first create W as an array, then on the next line you perform an operation and create a new variable also called W which is a float. In this instance it won't break your code, nevertheless it is better to create new variables and give them names that mean something.

Greg
  • 11,654
  • 3
  • 44
  • 50