How to create broken horizontal bar graphs in matplotlib?

A broken horizontal bar graph displays data as horizontal bars with gaps or breaks, useful for showing discontinuous time periods, interrupted processes, or segmented data. Matplotlib's broken_barh() method creates these visualizations effectively.

Syntax

ax.broken_barh(xranges, yrange, **kwargs)

Parameters

  • xranges − List of (start, width) tuples defining horizontal segments
  • yrange − Tuple (bottom, height) defining vertical position and height
  • facecolors − Colors for each segment (single color or tuple of colors)

Example

Let's create a broken horizontal bar graph showing project timelines with interruptions ?

import matplotlib.pyplot as plt

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

fig, ax = plt.subplots()

# Project 1: Two segments with gap
ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue')

# Project 2: Three segments with different colors
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
ax.grid(True)

# 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),
            fontsize=16,
            horizontalalignment='right', verticalalignment='top')

# Add labels
ax.set_xlabel('Time (days)')
ax.set_ylabel('Projects')
ax.set_yticks([14.5, 24.5])
ax.set_yticklabels(['Project 1', 'Project 2'])

plt.show()

The output shows horizontal bars with breaks representing project timelines ?

A horizontal bar chart with broken segments showing two projects with gaps and interruptions

Multiple Projects Example

Here's a more practical example showing multiple project phases ?

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10, 6))

# Project A: Design, Development, Testing phases
ax.broken_barh([(0, 20), (30, 40), (80, 15)], (10, 8), 
               facecolors=('lightblue', 'lightgreen', 'lightcoral'))

# Project B: Planning, Implementation, Review phases  
ax.broken_barh([(5, 15), (25, 50), (85, 20)], (20, 8),
               facecolors=('gold', 'lightsteelblue', 'plum'))

# Project C: Research, Prototype, Production phases
ax.broken_barh([(10, 25), (45, 30), (90, 25)], (30, 8),
               facecolors=('orange', 'cyan', 'pink'))

# Formatting
ax.set_xlim(0, 120)
ax.set_ylim(5, 45)
ax.set_xlabel('Timeline (weeks)')
ax.set_ylabel('Projects')

# Custom y-axis labels
ax.set_yticks([14, 24, 34])
ax.set_yticklabels(['Project A', 'Project B', 'Project C'])

ax.grid(True, alpha=0.3)
ax.set_title('Project Timeline with Phases')

plt.tight_layout()
plt.show()
A comprehensive broken horizontal bar chart showing three projects with different colored phases

Key Features

  • Gaps − Natural representation of breaks or interruptions in data
  • Color coding − Different colors for segments to represent categories or phases
  • Annotations − Add labels and arrows to explain specific segments
  • Grid lines − Help readers interpret time scales and values

Conclusion

Broken horizontal bar graphs effectively visualize discontinuous data with gaps and segments. Use broken_barh() with tuple lists for x-ranges and color coding to create clear timeline visualizations with interruptions or phases.

Updated on: 2026-03-26T02:36:52+05:30

401 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements