How to plot a stacked bar chart with custom labels and legend with Matplotlib

Torsten Scholz
3 min readFeb 23, 2022

--

I love to eat. I love cheese. I love tomato sauce. I love all kinds of exotic meals. But pastries… Those are to die for. So naturally my dream is to own a nice little bakery somewhere in a nice neighbourhood some day in the future, where I can share my passion for nice, sweet delicacies. Some very distant day in the future that is though...

But you did not click on this article due to your interest in my eating preferences. No. You are interested in creating majestic looking charts created with the Python library “Matplotlib.” Although both pastries and good charts have one thing in common: They look amazing and whet the appetite for more.

We are going to create a stacked bar chart with the help of the Python library “Matplotlib.” The chart is very basic and propably can be much improved. I am still learning and writing Medium articles is the first step for me in getting better.

Scenario

We have the following scenario at our hands. We are the owner of a restaurent (not my nice little bakery, yet, sadly) and want to visualize the average number of customers per day (Tuesday to Sunday; Monday is our weekly rest day) based on their meal time habit (that is whether they eat breakfast, lunch or dinner).

So the requirements for our chart are as follows:

  • Display the day of the week
  • Display the number of total average customers on that day
  • Display the number of average customers on that day by meal time
  • Make it easy to read the chart

Create the chart

Our final result will look like this:

The target chart satisfying all requirements. The legend categorizes the number of customers into the specific meal time and thus makes it easy to understand, how many customers come to eat for which meal on which day of the week.
The target chart satisfying all requirements. The legend categorizes the number of customers into the specific meal time and thus makes it easy to understand how many customers come to eat for which meal on which day of the week.

We are working with a very small dataset containing the average number of customers in our restaurant. We load the file and take a peek at its content.

Notice how the data is kind of “ugly”. That is the column names are not very descriptive (“b” stands for “breakfast”, “l” for “lunch” and “d” for dinner) and the days are abbreviated. We are not going to fix this in our dataframe that we will use but instead use customized labels when displaying the chart.

As we can see we have six rows containing data: One row for each day the restaurant is open (remember that on mondays we are closed).

Now it is time to plot our chart!

Remember that this is our result:

The target chart satisfying all requirements. The legend categorizes the number of customers into the specific meal time and thus makes it easy to understand, how many customers come to eat for which meal on which day of the week.
The target chart satisfying all requirements. The legend categorizes the number of customers into the specific meal time and thus makes it easy to understand how many customers come to eat for which meal on which day of the week.

There you have it. A nice, clean chart displaying all of our average number of customers per day based on their meal time habits. We can easily read the total number of customers per day by looking at the total height of each bar. We also can easily see how many customers we serve each meal time of the day. We changed the x-labels to their full day names (e. g., “tue” becomes “Tuesday”). As the column names in the data are not descriptive as mentioned above, we changed the legend to display a more accessible name.

I hope that this very short code snippet can be of help. Have a nice day.

--

--

Torsten Scholz

Former Software Developer, now Technical Lead in Münster, Germany. I like product development as well as optimizing organizational development processes.