Matplotlib Horizontal Bar Chart

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to create a horizontal bar chart using the Python Matplotlib library. A horizontal bar chart is a chart that displays data as horizontal bars. It is useful for comparing data across different categories.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FileHandlingGroup -.-> python/file_reading_writing("`Reading and Writing Files`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} matplotlib/importing_matplotlib -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} matplotlib/figures_axes -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} python/tuples -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} python/importing_modules -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} python/standard_libraries -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} python/file_reading_writing -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} python/math_random -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} python/numerical_computing -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} python/data_visualization -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} python/build_in_functions -.-> lab-48581{{"`Matplotlib Horizontal Bar Chart`"}} end

Import Required Libraries

The first step is to import the required libraries. We will be using numpy and matplotlib libraries in this lab.

import matplotlib.pyplot as plt
import numpy as np

Set the Random Seed

Before creating the bar chart, we need to set the random seed to ensure that we get the same results every time we run the code.

np.random.seed(19680801)

Create the Figure and Axes Objects

The next step is to create the figure and axes objects. The figure object is the window or the canvas where the chart is drawn, and the axes object is the actual chart.

fig, ax = plt.subplots()

Prepare the Data

The data for the chart is prepared in this step. We will create a list of people's names, their performance, and the error rate.

people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))

Create the Bar Chart

Finally, we will create the horizontal bar chart using the barh() method of the axes object.

ax.barh(y_pos, performance, xerr=error, align='center')

Customize the Chart

To make the chart more informative, we can customize it by adding labels, title, and by inverting the y-axis.

ax.set_yticks(y_pos, labels=people)
ax.invert_yaxis()  ## labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')

Show the Chart

Finally, we will show the chart by calling the show() method of the pyplot object.

plt.show()

Summary

In this lab, we learned how to create a horizontal bar chart using Python Matplotlib. We saw how to prepare data, create the figure and axes objects, and customize the chart. We also learned about the barh() method of the axes object and how to show the chart using the show() method of the pyplot object.