Skip to content

Line graph of cumulative sums over time aggregated by categorical value #2535

Answered by jakevdp
jimmywan asked this question in Q&A
Discussion options

You must be logged in to vote

It should look pretty much exactly like the example you linked to, except with a groupby in the window transform and an additional color encoding. Here's a short example:

import pandas as pd
import altair as alt

data = pd.DataFrame({
  'qtr': ['2016Q1', '2016Q2', '2016Q3', '2016Q4', '2017Q1', '2017Q2', '2017Q3', '2017Q4',
          '2016Q1', '2016Q2', '2016Q3', '2016Q4', '2017Q1', '2017Q2', '2017Q3', '2017Q4'],
  'company': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
              'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B'],
  'value': [5, 4, 5, 6, 4, 7, 8, 3,
            7, 8, 8, 9, 6, 5, 8, 7],
})

alt.Chart(data).transform_window(
    cumulative='sum(value)',
    sort=[{"field": "qtr"}],
    g…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@jimmywan
Comment options

@jakevdp
Comment options

@jimmywan
Comment options

Answer selected by jimmywan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants