Stacked Line Plots

Stacked Line plots work in the same way traditional charts are rendered. However, they stack multiple data points together to show both size of difference and specific values. This type of chart is useful when you want to show the contribution of individual datasets to the total value at each point along the x-axis.

Here's a simple example of how to create a stacked line chart in Pygal

import pygal
import numpy as np 

np.random.seed(42)

# Number of categories
num_categories = 3

# Number of data points
num_points = 20

# Generate random data for each category
data = np.random.rand(num_categories, num_points) 

data
array([[0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864, 0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258, 0.02058449, 0.96990985, 0.83244264, 0.21233911, 0.18182497, 0.18340451, 0.30424224, 0.52475643, 0.43194502, 0.29122914], [0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998, 0.78517596, 0.19967378, 0.51423444, 0.59241457, 0.04645041, 0.60754485, 0.17052412, 0.06505159, 0.94888554, 0.96563203, 0.80839735, 0.30461377, 0.09767211, 0.68423303, 0.44015249], [0.12203823, 0.49517691, 0.03438852, 0.9093204 , 0.25877998, 0.66252228, 0.31171108, 0.52006802, 0.54671028, 0.18485446, 0.96958463, 0.77513282, 0.93949894, 0.89482735, 0.59789998, 0.92187424, 0.0884925 , 0.19598286, 0.04522729, 0.32533033]])

Stacked Line Plot fill=True

Adding the argument fill= True add color fill to the plots to make the contributions more easily readable.

line = pygal.StackedLine( fill=True )
line.title = "Sales Over Time (in millions)"

line.add('TV/Radio', data[0])
line.add('Website', data[1])
line.add('Stores', data[2])

line.render_in_browser()
Sales Over Time (in millions) 0.2 0.2 0.4 0.4 0.6 0.6 0.8 0.8 1 1 1.2 1.2 1.4 1.4 1.6 1.6 1.8 1.8 2 2 Sales Over Time (in millions) 0.3745401188 12.36153846153846 434.44287877487085 0.9507143064 44.89190283400809 289.09873342393973 0.7319939418 77.42226720647771 344.27254222974176 0.5986584842 109.95263157894735 377.9073848698101 0.1560186404 142.48299595141697 489.5665170863107 0.1559945203 175.01336032388664 489.5726015592045 0.05808361217 207.54372469635624 514.2713429735219 0.8661761458 240.07408906882588 310.42410164116325 0.6011150117 272.6044534412955 377.2877078616472 0.7080725778 305.13481781376515 350.30688094795016 0.0205844943 337.66518218623474 523.7307692307693 0.9699098522 370.19554655870445 284.2565168649454 0.8324426408 402.72591093117404 318.93362455289514 0.2123391107 435.2562753036437 475.3592672842672 0.1818249672 467.78663967611334 483.0566827528354 0.1834045099 500.317004048583 482.65823158835707 0.304242243 532.8473684210526 452.1760311990107 0.5247564316 565.3777327935222 396.5497171741258 0.4319450186 597.9080971659918 419.9620736993772 0.2912291402 630.4384615384615 455.45868137678895 0.9863930136 (+0.6118528947) 12.36153846153846 280.0985190594113 1.090208167 (+0.1394938607) 44.89190283400809 253.91038865628775 1.02413859 (+0.2921446485) 77.42226720647771 270.5769217441581 0.9650203275 (+0.3663618433) 109.95263157894735 285.48993516076297 0.6120886247 (+0.4560699842) 142.48299595141697 374.51953388725667 0.9411704817 (+0.7851759614) 175.01336032388664 291.50623287729707 0.2577573943 (+0.1996737822) 207.54372469635624 463.90217364372785 1.380410584 (+0.5142344384) 240.07408906882588 180.70471022339348 1.193529581 (+0.5924145689) 272.6044534412955 227.8468077599817 0.7545229905 (+0.04645041272) 305.13481781376515 338.5894252136925 0.6281293462 (+0.6075448519) 337.66518218623474 370.47314476909423 1.140433976 (+0.1705241237) 370.19554655870445 241.24056170059674 0.8974942338 (+0.06505159299) 402.72591093117404 302.523885293037 1.161224648 (+0.9488855373) 435.2562753036437 235.99596287908372 1.147457 (+0.9656320331) 467.78663967611334 239.46895251376327 0.991801858 (+0.8083973481) 500.317004048583 278.73409857052735 0.6088560121 (+0.3046137692) 532.8473684210526 375.3349839977724 0.6224285456 (+0.09767211401) 565.3777327935222 371.9112133304677 1.116178045 (+0.6842330265) 597.9080971659918 247.35929730635547 0.7313816339 (+0.4401524937) 630.4384615384615 344.42700136124165 1.108431248 (+0.1220382348) 12.36153846153846 249.313483341849 1.585385077 (+0.4951769101) 44.89190283400809 128.99839788817707 1.058527111 (+0.03438852112) 77.42226720647771 261.902166228785 1.87434073 (+0.9093204021) 109.95263157894735 56.10722496089505 0.8708686063 (+0.2587799816) 142.48299595141697 309.24039422517194 1.603692766 (+0.6625222844) 175.01336032388664 124.38014969967861 0.5694684704 (+0.3117110761) 207.54372469635624 385.27077895682896 1.900478605 (+0.5200680212) 240.07408906882588 49.51375496347498 1.74023986 (+0.5467102793) 272.6044534412955 89.93514836775853 0.939377446 (+0.1848544555) 305.13481781376515 291.9585392201187 1.597713974 (+0.9695846278) 337.66518218623474 125.88834366232692 1.915566799 (+0.7751328234) 370.19554655870445 45.70764791027858 1.836993175 (+0.9394989416) 402.72591093117404 65.52841818019192 2.056051998 (+0.8948273504) 435.2562753036437 10.269230769230717 1.745356979 (+0.5978999788) 467.78663967611334 88.6443177045158 1.913676093 (+0.921874235) 500.317004048583 46.18459235730171 0.6973485142 (+0.08849250205) 532.8473684210526 353.0121042882307 0.8184114081 (+0.1959828624) 565.3777327935222 322.4731054515943 1.161405334 (+0.04522728891) 597.9080971659918 235.95038348282856 1.056711965 (+0.3253303308) 630.4384615384615 262.3600502512723 TV/Radio Website Stores

Stacked Line Plot fill=False

Alternatively, fill= False generates a less involved plot.

line = pygal.StackedLine( fill=False)
line.title = "Sales Over Time (in millions)"

line.add('TV/Radio', data[0])
line.add('Website', data[1])
line.add('Stores', data[2])

line.render_in_browser()
Sales Over Time (in millions) 0.2 0.2 0.4 0.4 0.6 0.6 0.8 0.8 1 1 1.2 1.2 1.4 1.4 1.6 1.6 1.8 1.8 2 2 Sales Over Time (in millions) 0.3745401188 12.36153846153846 434.44287877487085 0.9507143064 44.89190283400809 289.09873342393973 0.7319939418 77.42226720647771 344.27254222974176 0.5986584842 109.95263157894735 377.9073848698101 0.1560186404 142.48299595141697 489.5665170863107 0.1559945203 175.01336032388664 489.5726015592045 0.05808361217 207.54372469635624 514.2713429735219 0.8661761458 240.07408906882588 310.42410164116325 0.6011150117 272.6044534412955 377.2877078616472 0.7080725778 305.13481781376515 350.30688094795016 0.0205844943 337.66518218623474 523.7307692307693 0.9699098522 370.19554655870445 284.2565168649454 0.8324426408 402.72591093117404 318.93362455289514 0.2123391107 435.2562753036437 475.3592672842672 0.1818249672 467.78663967611334 483.0566827528354 0.1834045099 500.317004048583 482.65823158835707 0.304242243 532.8473684210526 452.1760311990107 0.5247564316 565.3777327935222 396.5497171741258 0.4319450186 597.9080971659918 419.9620736993772 0.2912291402 630.4384615384615 455.45868137678895 0.9863930136 (+0.6118528947) 12.36153846153846 280.0985190594113 1.090208167 (+0.1394938607) 44.89190283400809 253.91038865628775 1.02413859 (+0.2921446485) 77.42226720647771 270.5769217441581 0.9650203275 (+0.3663618433) 109.95263157894735 285.48993516076297 0.6120886247 (+0.4560699842) 142.48299595141697 374.51953388725667 0.9411704817 (+0.7851759614) 175.01336032388664 291.50623287729707 0.2577573943 (+0.1996737822) 207.54372469635624 463.90217364372785 1.380410584 (+0.5142344384) 240.07408906882588 180.70471022339348 1.193529581 (+0.5924145689) 272.6044534412955 227.8468077599817 0.7545229905 (+0.04645041272) 305.13481781376515 338.5894252136925 0.6281293462 (+0.6075448519) 337.66518218623474 370.47314476909423 1.140433976 (+0.1705241237) 370.19554655870445 241.24056170059674 0.8974942338 (+0.06505159299) 402.72591093117404 302.523885293037 1.161224648 (+0.9488855373) 435.2562753036437 235.99596287908372 1.147457 (+0.9656320331) 467.78663967611334 239.46895251376327 0.991801858 (+0.8083973481) 500.317004048583 278.73409857052735 0.6088560121 (+0.3046137692) 532.8473684210526 375.3349839977724 0.6224285456 (+0.09767211401) 565.3777327935222 371.9112133304677 1.116178045 (+0.6842330265) 597.9080971659918 247.35929730635547 0.7313816339 (+0.4401524937) 630.4384615384615 344.42700136124165 1.108431248 (+0.1220382348) 12.36153846153846 249.313483341849 1.585385077 (+0.4951769101) 44.89190283400809 128.99839788817707 1.058527111 (+0.03438852112) 77.42226720647771 261.902166228785 1.87434073 (+0.9093204021) 109.95263157894735 56.10722496089505 0.8708686063 (+0.2587799816) 142.48299595141697 309.24039422517194 1.603692766 (+0.6625222844) 175.01336032388664 124.38014969967861 0.5694684704 (+0.3117110761) 207.54372469635624 385.27077895682896 1.900478605 (+0.5200680212) 240.07408906882588 49.51375496347498 1.74023986 (+0.5467102793) 272.6044534412955 89.93514836775853 0.939377446 (+0.1848544555) 305.13481781376515 291.9585392201187 1.597713974 (+0.9695846278) 337.66518218623474 125.88834366232692 1.915566799 (+0.7751328234) 370.19554655870445 45.70764791027858 1.836993175 (+0.9394989416) 402.72591093117404 65.52841818019192 2.056051998 (+0.8948273504) 435.2562753036437 10.269230769230717 1.745356979 (+0.5978999788) 467.78663967611334 88.6443177045158 1.913676093 (+0.921874235) 500.317004048583 46.18459235730171 0.6973485142 (+0.08849250205) 532.8473684210526 353.0121042882307 0.8184114081 (+0.1959828624) 565.3777327935222 322.4731054515943 1.161405334 (+0.04522728891) 597.9080971659918 235.95038348282856 1.056711965 (+0.3253303308) 630.4384615384615 262.3600502512723 TV/Radio Website Stores