There's more...

Another way of presenting stacked bars is adding them as percentages, so the total doesn't change, only the relative sizes compared to each other.

To do that, VALUESA and VALUEB need to be calculated relative to the percentages in this way:

>>> VALUESA = [100 * valueA / (valueA + valueB) for label, valueA, valueB in DATA]
>>> VALUESB = [100 * valueB / (valueA + valueB) for label, valueA, valueB in DATA]

This makes each value equal to the percentage of the total, and the total always adds up to 100. This produces the following graphic:

The bars doesn't necessarily need to be stacked. Sometimes, it may be interesting to present the bars one against the other for comparison. 

To do that, we need to move the position of the second bar sequence. We'll need also to set thinner bars to allow space:

>>> WIDTH = 0.3
>>> plt.bar([p - WIDTH / 2 for p in POS], VALUESA, width=WIDTH)
>>> plt.bar([p + WIDTH / 2 for p in POS], VALUESB, width=WIDTH)

Note how the width of the bar is set to a third of the space, as our reference space is 1 between the bars. The first bar is moved to the left and the second to the right to center them. The bottom argument has been deleted to not stack the bars:

 

The full matplotlib documentation can be found here: https://matplotlib.org/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.186.115