Create Broken Horizontal Bar Plots with Matplotlib

MatplotlibMatplotlibBeginner
Practice Now

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

Introduction

Matplotlib is a Python library used for creating static, animated, and interactive visualizations in Python. It is a popular library used for data visualization in Python. In this tutorial, we will learn how to create a broken horizontal bar plot using Matplotlib.

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 matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") matplotlib/PlotCustomizationGroup -.-> matplotlib/text_annotations("`Text Annotations`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} matplotlib/grid_config -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} matplotlib/text_annotations -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} python/variables_data_types -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} python/booleans -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} python/lists -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} python/tuples -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} python/importing_modules -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} python/data_collections -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} python/data_visualization -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} python/build_in_functions -.-> lab-48593{{"`Create Broken Horizontal Bar Plots with Matplotlib`"}} end

Import the necessary libraries

In this step, we will import the necessary libraries. We will be using the matplotlib.pyplot library to create the broken horizontal bar plot.

import matplotlib.pyplot as plt

Create the broken horizontal bar plot

In this step, we will create the broken horizontal bar plot. We will be using the broken_barh() method of the Axes class to create the plot. The broken_barh() method takes three arguments: the first argument is a list of tuples where each tuple represents a segment of the bar and the first element of the tuple is the starting point of the segment and the second element is the length of the segment; the second argument is the y-coordinate of the bar; and the third argument is the face color of the bar.

fig, ax = plt.subplots()
ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue')
ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9),
               facecolors=('tab:orange', 'tab:green', 'tab:red'))
ax.set_ylim(5, 35)
ax.set_xlim(0, 200)
ax.set_xlabel('seconds since start')
ax.set_yticks([15, 25], labels=['Bill', 'Jim'])
ax.grid(True)
ax.annotate('race interrupted', (61, 25),
            xytext=(0.8, 0.9), textcoords='axes fraction',
            arrowprops=dict(facecolor='black', shrink=0.05),
            fontsize=16,
            horizontalalignment='right', verticalalignment='top')

plt.show()

Interpret the plot

In this step, we will interpret the broken horizontal bar plot. The plot represents a race where two participants, Bill and Jim, started at different times. The y-axis represents the participants, and the x-axis represents the time since the start of the race in seconds. The blue and orange bars represent Bill's race, while the green, red, and light blue bars represent Jim's race. The annotation "race interrupted" indicates that the race was interrupted at 61 seconds.

Summary

Matplotlib is a popular library used for data visualization in Python. In this tutorial, we learned how to create a broken horizontal bar plot using Matplotlib. We imported the necessary libraries, created the plot using the broken_barh() method of the Axes class, and interpreted the plot.