Search code examples
pythongraphpygal

Combine and compare two sets of stacked data in pygal


I'm wondering if it's possible to combine two sets of data (made up of two lists each) into a pygal chart.

The code would look something like this:

new_chart = pygal.StackedBar()

# set 1
new_chart.add('1-1',[1,2,3,4])
new_chart.add('1-2',[4,3,2,1])

# set 2
new_chart.add('2-1',[9,8,7,6])
new_chart.add('2-2',[6,7,8,9])

new_chart.render()

But I would want the second set to be next to (not stacked on) the first set.


Solution

  • That kind of does the trick :

    new_chart = pygal.StackedBar()
    
    # set 1
    new_chart.add('1-1',[1, 0, 2, 0, 3, 0, 4, 0])
    new_chart.add('1-2',[4, 0, 3, 0, 2, 0, 1, 0])
    
    # set 2
    new_chart.add('2-1',[0, 9, 0, 8, 0, 7, 0, 6])
    new_chart.add('2-2',[0, 6, 0, 7, 0, 8, 0, 9])
    

    The output image

    You can get a clearer image with additional zeros for empty spaces :

    new_chart = pygal.StackedBar()
    # set 1
    new_chart.add('1-1',[1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0])
    new_chart.add('1-2',[4, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0])
    
    # set 2
    new_chart.add('2-1',[0, 9, 0, 0, 8, 0, 0, 7, 0, 0, 6])
    new_chart.add('2-2',[0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9])