Create Grouped Bar Chart using Altair in Python
Grouped bar charts are a handy tool to represent our data when we want to compare multiple sets of data items one against another. To make a grouped bar chart, we require at least three rows of three columns of data in our dataset. The three columns can be used as- one for values, one for series, and one for categories. In a grouped bar chart, the data values present in a series are represented side by side and are grouped under specific categories so that their values can be represented accordingly over the axis. All the data values belonging to a specific series will always be represented with the same color across all the categories.
Let’s take an example, suppose, we want to compare the runs made by two players across three formats. Here, runs scored by the players act as values, Player name acts as a series and the format of the game acts as the categories. Always, the runs scored by each player will have the same color representation across the different formats.
In this article, we will learn to create a grouped bar chart using Altair library in Python.
As we discussed, we need at least three rows/columns, we will begin with importing necessary libraries and then creating a dataset having three columns using pandas library.
Now, that we have a dataset containing three columns, where we want to compare the highest score (values) of two players (series) across different formats (categories).
We can plot a group chart by calling the alt.Chart() method present inside Altair library. We want our categories to be displayed as the columns so, we can pass Format inside the alt.Column() method, our series (players) will be represented on the x-axis hence we pass Player in the alt.X() field, and our values (runs) should be displayed on y-axis so we pass Highest Score inside the alt.Y() field. Each player should have same color in all formats so, we pass Player inside the alt.Color() method.