Search code examples
pythonchartspygal

How do I add values to the bars in a Stacked Bar chart in Pygal?


I have a StackedBar chart created using pygal see below: enter image description here

And I would like to add labels to each of the stacked bars a bit like those from the documentation for Bar charts (see below .. from the pygal documentation). The method of adding these labels described in the documentation for Bar charts does not seem to work for StackedBar charts.

enter image description here

What I'm trying to get is the numeric values printed on the graph bars.


Solution

  • Pass the print_values argument when creating your StackedBar object. For example, the following code creates the chart shown below:

    import pygal
    chart = pygal.StackedBar(print_values=True)
    chart.add("Series A", [None, None, 40, 20, 10])
    chart.add("Series B", [80, 60, 40, 40, 30])
    chart.add("Series C", [20, 40, 20, 40, 60])
    chart.render()
    

    Stacked bar chart with labels

    The different parameters that can be used when creating chart objects are listed in the chart configuration section of the docs.