changeset 297:f6f423e25c7f

adding function to generate step plots (for cumulative number of vehicles)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 11 Feb 2013 17:35:37 -0500
parents 586ead03fc00
children 4a8b6a2de82f
files python/tests/utils.txt python/utils.py
diffstat 2 files changed, 17 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/python/tests/utils.txt	Mon Feb 11 16:01:15 2013 -0500
+++ b/python/tests/utils.txt	Mon Feb 11 17:35:37 2013 -0500
@@ -41,6 +41,9 @@
 >>> values[-1]
 6.0
 
+>>> stepPlot([3, 5, 7, 8], 1, 10, 0)
+([1, 3, 3, 5, 5, 7, 7, 8, 8, 10], [0, 0, 1, 1, 2, 2, 3, 3, 4, 4])
+
 >>> LCSS(range(5), range(5), 0.1, lambda x,y:abs(x-y))
 5
 >>> LCSS(range(1,5), range(5), 0.1, lambda x,y:abs(x-y))
--- a/python/utils.py	Mon Feb 11 16:01:15 2013 -0500
+++ b/python/utils.py	Mon Feb 11 17:35:37 2013 -0500
@@ -245,6 +245,20 @@
 # plotting section
 #########################
 
+def stepPlot(X, firstX, lastX, initialCount = 0):
+    '''for each value in x, increment by one the initial count
+    returns the lists that can be plotted 
+    to obtain a step plot increasing by one for each value in x, from first to last value'''
+    
+    sortedX = []
+    counts = [initialCount]
+    for x in sorted(X):
+        sortedX += [x,x]
+        counts.append(counts[-1])
+        counts.append(counts[-1]+1)
+    counts.append(counts[-1])
+    return [firstX]+sortedX+[lastX], counts
+
 class PlottingPropertyValues:
     def __init__(self, values):
         self.values = values