Black Lives Matter. Please consider donating to Black Girls Code today.

[solved] Bar graph column ordering changes between streaming updates

I have a grouped bar chart that I’m pushing updates to via the streaming API once a second, and the bars are named ‘D1’, ‘D2’, ‘D2’, ‘D3’, ‘D4’, and ‘D5’. I want the bars to show up in the axes in that order. However, when the chart updates sometimes the order changes (D2 D3 D4 D5 D1), which is very confusing visually. This is the python code for the plot:

trace1 = go.Bar( x=['D1', 'D2', 'D3', 'D4', 'D5'],
                 y=[],
                 name='actual throughput',
                 stream=stream_1)         # (!) embed stream id, 1 per trace                                                                                                 

trace2 = go.Bar( x=['D1', 'D2', 'D3', 'D4', 'D5'],
                 y=[],
                 name='predicted throughput (before)',
                 stream=stream_2)         # (!) embed stream id, 1 per trace                                                                               

data = go.Data([trace1, trace2])
layout = go.Layout(barmode='group', title='Points n Stuff')
fig = go.Figure(data=data, layout=layout)
py.plot(fig, filename='python-streaming')

And this is the code for the streaming updates:

stream_ids = tls.get_credentials_file()['stream_ids']
stream_1 = go.Stream( token=stream_ids[0])                                                                                                          
stream_2 = go.Stream( token=stream_ids[1])     

s1 = py.Stream(stream_ids[0])
s2 = py.Stream(stream_ids[1])

#pen connections                                                                                                                                                   
s1.open()
s2.open()

i = 0    # a counter                                                                                                                                                         
k = 5    # some shape parameter                                                                                                                                             

Ds = ['D1', 'D2', 'D3', 'D4', 'D5']
while True:
    # Ds on x-axis, random numbers on y-axis                                                                                                                       
    for x in Ds:
        # Send random data to plot                                                                                                                                             
        s1.write(dict(x=x, y = abs((np.cos(k*i/50.)*np.cos(i/50.)+np.random.randn(1))[0])))
        s2.write(dict(x=x, y = abs((np.cos(k*i/50.)*np.cos(i/50.)+np.random.randn(1))[0])))                                                                                               
    time.sleep(1)  # plot a point every second                                                                                                                               

# Close the streams when done plotting                                                                                                                                       
s1.close()
s2.close()

Is there anything I can do to force the x-axis ordering to be fixed?

Thanks!

Edit: realized I was updating “point by point” instead of sending whole lists at a time. I changed x and y to be lists in the while loop and it solved my problem.