1
votes

I'm using plotly to display a stacked bar chart with cummulative counts of objects. For context, the x-axis is dates and y-axis is the count of some names. There are 5 people who do these counts on different days and I want to display the count each of them does on each day so the stacked bar chart comes into play. Now there are some days where no one has counted names so there is no data and there is a gap in the bar chart. Ideally, since this is a cummulative count, I'd like the count for these 'empty' days to be the same bar chart as the previous day so that the graph is continuous. How do I achieve this? Here is python code below :

    traces = []
    for r in pivot_data.index.tolist():
         trace = go.Bar(
         name = r,
         x=pivot_data.columns.tolist(), # these are all the dates in the dataset
         y=pivot_data.loc[r].fillna(0).values.cumsum().tolist(), # get values for each date
         )
     traces.append(trace)

    data = traces
    layout = go.Layout(title="Cummulative Name count per volunteer for each weekday", xaxis= 
                {'title':'Date'}, yaxis={'title':'Name count'}, xaxis_tickangle=-45,
               barmode='stack'
     )


   fig = go.Figure(data=data, layout=layout)
   iplot(fig)

The data frame is pivoted and here is how it looks with recipients being the index:

    pivot_data

                      Date  2019-06-01 00:00:00 2019-06-02 00:00:00 
    names                                                                                   
    Mark Jones                3.0                  NaN  
    Dennis Smith             NaN                   1.0

Let me know if any other information is required.

1
A complete code snippet along with sample data would be great!vestland

1 Answers

1
votes

I'd like the count for these 'empty' days to be the same bar chart as the previous day so that the graph is continuous.

It sounds to me that you could use a Multi Category Waterfall Chart:

enter image description here

Code:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Waterfall(
    x = [["2016", "2017", "2017", "2017", "2017", "2018", "2018", "2018", "2018"],
        ["initial", "q1", "q2", "q3", "total", "q1", "q2", "q3", "total"]],
    measure = ["absolute", "relative", "relative", "relative", "total", "relative", "relative", "relative", "total"],
    y = [1, 2, 3, 1, None, 1, 2, 4, None],
    base = 0,
    name = 'Mark Jones'
))

fig.add_trace(go.Waterfall(
    x = [["2016", "2017", "2017", "2017", "2017", "2018", "2018", "2018", "2018"],
        ["initial", "q1", "q2", "q3", "total", "q1", "q2", "q3", "total"]],
    measure = ["absolute", "relative", "relative", "relative", "total", "relative", "relative", "relative", "total"],
    y = [2, 2, 3, 1, None, 1, 2, 4, None],
    base = 0,
    name = 'Dennis Smith'
))

fig.update_layout(
    waterfallgroupgap = 0.5,
)

fig.show()