0
votes

I have a list that I generated with python from data in mysql. "select HOD, DOW, count(*) from mydb group by hod, dow order by 1,2; Each node represents hour of day (0-23), day of week(0-6), and count(1-n).

I am trying to use plotly to generate a stacked bar chart showing X-Axis as HOD, Y-Axis as count, and each partition of a stack as DOW/Count.

I have consulted the plotly site for examples but I have not figured out how to make it work for my situation. Can someone tell me what I am missing?

import plotly as pl
import plotly.graph_objs as go

myresult=[(0, 0, 61), (0, 1, 76), (0, 2, 46), (0, 3, 64), (0, 4, 70), (0, 5, 51), (0, 6, 48), (1, 0, 87), (1, 1, 47), (1, 2, 86), (1, 3, 57), (1, 4, 53), (1, 5, 84), (1, 6, 86), (2, 0, 40), (2, 1, 79), (2, 2, 54), (2, 3, 51), (2, 4, 71), (2, 5, 71), (2, 6, 35), (3, 0, 48), (3, 1, 58), (3, 2, 45), (3, 3, 52), (3, 4, 91), (3, 5, 49), (3, 6, 63), (4, 0, 56), (4, 1, 47), (4, 2, 61), (4, 3, 51), (4, 4, 62), (4, 5, 43), (4, 6, 72), (5, 0, 66), (5, 1, 24), (5, 2, 50), (5, 3, 45), (5, 4, 71), (5, 5, 62), (5, 6, 40), (6, 0, 50), (6, 1, 51), (6, 2, 64), (6, 3, 31), (6, 4, 68), (6, 5, 60), (6, 6, 75), (7, 0, 62), (7, 1, 50), (7, 2, 61), (7, 3, 59), (7, 4, 48), (7, 5, 53), (7, 6, 71), (8, 0, 87), (8, 1, 63), (8, 2, 49), (8, 3, 44), (8, 4, 90), (8, 5, 59), (8, 6, 52), (9, 0, 59), (9, 1, 69), (9, 2, 45), (9, 3, 54), (9, 4, 68), (9, 5, 65), (9, 6, 61), (10, 0, 79), (10, 1, 58), (10, 2, 60), (10, 3, 56), (10, 4, 49), (10, 5, 73), (10, 6, 41), (11, 0, 73), (11, 1, 40), (11, 2, 59), (11, 3, 79), (11, 4, 72), (11, 5, 37), (11, 6, 65), (12, 0, 66), (12, 1, 65), (12, 2, 48), (12, 3, 59), (12, 4, 64), (12, 5, 66), (12, 6, 56), (13, 0, 58), (13, 1, 62), (13, 2, 96), (13, 3, 59), (13, 4, 55), (13, 5, 40), (13, 6, 64), (14, 0, 56), (14, 1, 55), (14, 2, 71), (14, 3, 55), (14, 4, 71), (14, 5, 60), (14, 6, 56), (15, 0, 76), (15, 1, 45), (15, 2, 74), (15, 3, 55), (15, 4, 70), (15, 5, 49), (15, 6, 82), (16, 0, 51), (16, 1, 60), (16, 2, 53), (16, 3, 58), (16, 4, 59), (16, 5, 77), (16, 6, 55), (17, 0, 52), (17, 1, 37), (17, 2, 66), (17, 3, 61), (17, 4, 57), (17, 5, 46), (17, 6, 59), (18, 0, 62), (18, 1, 56), (18, 2, 53), (18, 3, 57), (18, 4, 87), (18, 5, 72), (18, 6, 68), (19, 0, 52), (19, 1, 67), (19, 2, 46), (19, 3, 68), (19, 4, 53), (19, 5, 76), (19, 6, 44), (20, 0, 83), (20, 1, 55), (20, 2, 68), (20, 3, 47), (20, 4, 59), (20, 5, 63), (20, 6, 73), (21, 0, 82), (21, 1, 66), (21, 2, 47), (21, 3, 53), (21, 4, 57), (21, 5, 47), (21, 6, 41), (22, 0, 37), (22, 1, 67), (22, 2, 59), (22, 3, 47), (22, 4, 49), (22, 5, 55), (22, 6, 32), (23, 0, 79), (23, 1, 87), (23, 2, 31), (23, 3, 53), (23, 4, 61), (23, 5, 54), (23, 6, 81)]

for row in myresult:
  xrow.append(row[1])
  xrow.append(row[2])
  yrow.append(row[0])

fig = go.Figure()
fig.add_trace(go.Bar(x=yrow3, y=xrow3, name='HOD'))

fig.update_layout(barmode='relative', title_text='Activity - DOW/HOD')
pl.offline.plot(fig, filename='stackedbar_hod_dow.html')

Thanks Dan

1

1 Answers

1
votes

List of tuples may be the worst choice you want to do here instead parse your data as a pandas dataframe or convert it:

import pandas as pd
df = pd.DataFrame(myresult).rename(columns=dict(zip(range(3),['DOW', 'HOD', 'COUNT'])))

Its because it makes it much easier to provide column names and have this functionality accordingly:

import plotly.express as px
fig = px.bar(df, x="DOW", y="COUNT", color ="HOD" )
fig.show()

enter image description here

Or

import plotly.express as px
fig = px.bar(df, x="HOD", y="COUNT", color ="DOW" )
fig.show()

enter image description here