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:
- the ts values will always be >= the previous value they will be epoch timestamps.
- 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
