Change the bar widths in a Matplotlib bar plot

Python

Matplotlib

Bar

Change bar widths

Luc B.

Python

Matplotlib

Bar Plot

Adjusting the bar width in a Matplotlib bar plot can make the plot significantly easier to interpret, especially in plots with high volumes of data that have very little spacing between bars. Furthermore, by changing the bar widths individually, one can use the bar width to visually encode an additional dimension of data.

Code Example

Use the width parameter to plt.bar() to change the bar widths in Matplotlib. width can be a scalar value to change the width universally or an array to change the width of each bar individually.

import matplotlib.pyplot as plt

labels = ['Amy', 'Rob', 'Dimitri', 'Sarah', 'Brian']
values = [60, 56, 63, 75, 48]

# Change the bar widths here
plt.bar(labels, values, width=[1, 0.5, 1, 0.5, 0.5])

plt.ylabel('Typing Speed (wpm)')
plt.show()

png

More Examples

Change the Bar Widths Universally

Change the bar widths universally by passing a scalar value to width.

# Change the bar width here
plt.bar(labels, values, width=0.1)

plt.ylabel('Typing Speed (wpm)')
plt.show()

png