Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Grouped Bars
course content

Course Content

Visualization in Python with matplotlib

Grouped BarsGrouped Bars

Instead of placing each new bar above the previous one, there is one more approach to visualize such data: placing bars on the sides of other bars. We call such bar charts grouped bar charts.

The approach of building grouped bars is similar, but with some nuances. First, we need to predefine column widths. Then, we need to generate a sequence of 'positions' (preferably by using np.arange()). Then, with every call of the .bar() function we pass the newly created sequence as the first argument, and the values as the second. The third positional argument should be the column width. Then, for better readability, we should set the label parameter.

Please note, that for building 2 columns we need to set x - width/2 as the first parameter with the first .bar() call, and x + width/2 for the second. If we want to build three columns, we can use the following approach: x - width, x, and x + width.

We may see, that there we created a numpy array length of the same as countries, and then used approach from the note above. Also, we used the plt.xticks function to display countries on the x-axis ticks.

Everything was clear?

Section 2. Chapter 5