How to control the border of a bar patch in matplotlib?

In matplotlib, you can control the appearance of bar chart borders using several parameters in the bar() method. The main parameters are edgecolor for border color and linewidth for border thickness.

Basic Border Control

Use edgecolor to set the border color of bar patches ?

import numpy as np
import matplotlib.pyplot as plt

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

heights = [3, 12, 5, 18, 45]
labels = ('P1', 'P2', 'P3', 'P4', 'P5')
x_pos = np.arange(len(labels))

plt.bar(x_pos, heights, color=(0.9, 0.7, 0.1, 0.5), edgecolor='green')
plt.xticks(x_pos, labels)
plt.title('Bar Chart with Green Borders')

plt.show()

Controlling Border Width

Use linewidth parameter to control the thickness of bar borders ?

import numpy as np
import matplotlib.pyplot as plt

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

heights = [3, 12, 5, 18, 45]
labels = ('P1', 'P2', 'P3', 'P4', 'P5')
x_pos = np.arange(len(labels))

plt.bar(x_pos, heights, color=(0.9, 0.7, 0.1, 0.5), 
         edgecolor='green', linewidth=5)
plt.xticks(x_pos, labels)
plt.title('Bar Chart with Thick Borders')

plt.show()

Different Border Styles

You can create various border effects by combining different parameters ?

import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(1, 3, figsize=(15, 4))
fig.suptitle('Different Border Styles')

heights = [3, 12, 5, 18]
labels = ('A', 'B', 'C', 'D')
x_pos = np.arange(len(labels))

# No borders
axes[0].bar(x_pos, heights, color='skyblue', edgecolor='none')
axes[0].set_title('No Borders')
axes[0].set_xticks(x_pos)
axes[0].set_xticklabels(labels)

# Black borders
axes[1].bar(x_pos, heights, color='lightcoral', edgecolor='black', linewidth=2)
axes[1].set_title('Black Borders')
axes[1].set_xticks(x_pos)
axes[1].set_xticklabels(labels)

# Multiple colors
colors = ['red', 'blue', 'green', 'orange']
axes[2].bar(x_pos, heights, color=colors, edgecolor='white', linewidth=3)
axes[2].set_title('White Borders')
axes[2].set_xticks(x_pos)
axes[2].set_xticklabels(labels)

plt.tight_layout()
plt.show()

Key Parameters

Parameter Purpose Example Values
edgecolor Border color 'black', 'red', 'none'
linewidth Border thickness 1, 2, 5 (pixels)
linestyle Border style '-', '--', ':'

Conclusion

Use edgecolor to set border colors and linewidth to control border thickness in matplotlib bar charts. Set edgecolor='none' to remove borders completely.

Updated on: 2026-03-26T15:04:50+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements