Add space between histogram bars in Matplotlib

Python

Matplotlib

Histogram

Change bar width

Luc B.

Python

Matplotlib

Histogram

By default, Matplotlib's histograms lack division or space between adjacent bars. This makes the bars blend together so they're difficult to interpret. There are a couple ways to address this including changing the bar width or adding a border around the bars.

Code Example

Use the rwidth parameter to plt.hist() to change the bar width. This value specifies the bar width as a fraction of the default width, and thus it cannot be larger than 1.

import matplotlib.pyplot as plt

values = [1, 1, 2, 3, 8, 7, 5, 4, 1, 3, 2, 4, 5, 8, 9]

# Adjust the bar widths here
plt.hist(values, rwidth=0.9)

plt.ylabel("Quantity")
plt.xlabel("Value")
plt.show()

png