Home > Software engineering >  A algorithm to manipulate lists for a bokeh stacked bar graph
A algorithm to manipulate lists for a bokeh stacked bar graph

Time:01-06

I am receiving API data that I'm trying to work into a Bokeh stacked bar graph. The Bokeh documentation shows that I require this format:

data = {
    x : [1, 2, 3, 4, 5],
    y1 : [1, 2, 4, 3, 4],
    y2 : [1, 4, 2, 2, 3],
}

My data comes out of my for loop like this:

ts = [0, 1,   1,   1,  1, 2,  2,   4,  4,   4]
tx = [0, 233, 121, 34, 4, 22, 522, 11, 245, 3]

and therefore needs to end up like this:

# the xlist = the unique values of ts
# the number of layers below = count (4) of most common element of ts (1)
# the length of each layer list = the number of unique elements (4) of ts (0,1,2,4)

data = {
    xlist  : [0, 1,   2,   4]
    layer0 : [0, 233, 22,  11]
    layer1 : [0, 121, 522, 245]
    layer2 : [0, 34,  0,   3]
    layer3 : [0, 4,   0,   0]
}

Givens:

  1. the ts values will always be >= the previous value they will be epoch timestamps.
  2. there will (should) always be 2 lists of equal length.

This gets me the skeleton of the dictionary and so I feel like i'm close, but the logic in the loops is just outside of my grasp. If anyone can help with a best practices approach to this, I'd appreciate it.

from statistics import mode

def stacker(list1, list2):
   data={}
   xlist = list(set(list1))
   if len(list1) != len(list2):
      layer0 = [0]*len(list1)
      data=dict(xlist, layer0)
      print("lists weren't equal length")
      return data
   data['xlist'] = xlist
   modeVal = mode(list1)
   layercount = list1.count(modeVal)
   for i in range(layercount):
      data['layer'   str(i)] = []
      # loops
   return data

CodePudding user response:

Is it alright to use numpy? You must configure the num_layers variable, I have set it to 4 to be the same in the example. Here is my attempt:

import numpy as np


ts = [0, 1,   1,   1,  1, 2,  2,   4,  4,   4]
tx = [0, 233, 121, 34, 4, 22, 522, 11, 245, 3]


xlist = list(set(ts))
ylist = []

i_list = []
for x in xlist:
    for i in range(len(ts)):
        
        if x == ts[i]:
            #print(x, tx[i])
            i_list.append(tx[i])
    
    ylist.append(i_list)
    i_list =[]

num_layers = 4

mod_ylist = []
for element in ylist:
    mod_ylist.append(element [0]*(num_layers - len(element)))

final = np.array(mod_ylist).T


data = {'xlist' : xlist}
for i in range(len(final)):
    data['layer'   str(i)] = final[i].tolist()

print(data)

Here is the output:

{'xlist': [0, 1, 2, 4], 'layer0': [0, 233, 22, 11], 'layer1': [0, 121, 522, 245], 'layer2': [0, 34, 0, 3], 'layer3': [0, 4, 0, 0]}

Here is the output directly from console, after I have pretty printed too. Note that the entries are out of order because dictionaries are unordered.

enter image description here

  •  Tags:  
  • Related