How to create broken horizontal bar graphs in matplotlib?

To create broken horizontal bar graphs in matplotlib, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Create a figure and a set of subplots.
  • Plot a horizontal sequence of rectangles.
  • Scale X and Y axes limit.
  • Configure the grid lines.
  • Annotate the broken bars.
  • To display the figure, use show() method.


import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, ax = plt.subplots()

# Horizontal sequence of rectangles
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'))

# Scale X and Y axes limits
ax.set_ylim(5, 35)
ax.set_xlim(0, 200)

# Configure the grid lines

# Annotate the broken bars
ax.annotate('race interrupted', (61, 25),
   xytext=(0.8, 0.9), textcoords='axes fraction',
   arrowprops=dict(facecolor='black', shrink=0.05),
   horizontalalignment='right', verticalalignment='top')


It will produce the following output

Updated on: 22-Sep-2021


Kickstart Your Career

Get certified by completing the course

Get Started