Frequency distributions and graphing

Blog Post created by Dan_Patterson on Aug 18, 2014

A simple verbose demo again, so that I don't forget.

I updated it to conform to python 3.x... which will inevitably hit ArcMap.

The script is pretty self explanatory and no effort has been made to simplify it.

``""" CollectionsDemo.py Author:   Dan.Patterson@carleton.ca Purpose:   To demonstrate the utility of using the collections module to   obtain a simple frequency distribution, in this case, a list   of random integers.  It is written verbosely so that the user   can see the sequence of events and the results of the various   methods.   A sequence of random numbers is generated and a dictionary of   key:values is produced by collections.Counter.  The resultant   keys are cloned to "classes" to prevent alteration of the   initial keys.  An extra class is appended to the list to ensure   that the last class in the keys is included since the behaviour   of histogram is to combine the last two classes into   one frequency (long story).  I just add a value of 1 to the last   class to produce an extra bin.   A histogram is produced which contains the classes and the frequency   for those classes.  """  import collections  import random  import numpy as np  from matplotlib import pyplot as plt    rand_int = [random.randrange(1,6) for i in range(15)]  dict = collections.Counter(rand_int)  keys = dict.keys()  counts = dict.values()  classes = list(keys)                #clone the keys  classes.append(classes[-1] + 1)  #to ensure that the last bin has values  #  histo = np.histogram(rand_int,classes)#args = [rand_int, dict, keys, counts, histo, histo[1], histo[0]]  frmt = """Collections and pylabRandom integers: {} Collections dict:     {}  keys:           {}  values (freq):  {}  Histogram         {}    classes:       {}    frequency:     {} """print(frmt.format(*args))#plt.hist(rand_int,bins=classes,align='left')  plt.title("Sample Histogram", loc='center')  plt.xlabel("class"); plt.ylabel("frequency")  plt.show()plt.close()``

Results

``Collections and pylabRandom integers: [4, 5, 3, 4, 2, 4, 1, 5, 4, 5, 4, 5, 2, 2, 3] Collections dict:     Counter({4: 5, 5: 4, 2: 3, 3: 2, 1: 1})  keys:           dict_keys([1, 2, 3, 4, 5])  values (freq):  dict_values([1, 3, 2, 5, 4])  Histogram         (array([1, 3, 2, 5, 4]), array([1, 2, 3, 4, 5, 6]))    classes:       [1 2 3 4 5 6]    frequency:     [1 3 2 5 4] ``

As a simple histogram.

Which of course can be fancied up to suit your needs.  Matplotlib is certainly one package to explore... and there are even high-end graphics modules.