Can matplotlib have rounded edges for bars?
- User
Can matplotlib have rounded edges for bars?
- Assistant
Yes — you can give rounded edges to bars in Matplotlib, but not directly through the default plt.bar() parameters. Instead, you have a few approaches:
- Using FancyBboxPatch for rounded rectangles You can replace the default Rectangle patches that plt.bar() creates with rounded boxes: pythonCopiar códigoimport matplotlib.pyplot as plt from matplotlib.patches import FancyBboxPatch
Sample data
x = [0, 1, 2] y = [3, 5, 2]
fig, ax = plt.subplots()
Plot bars normally (invisible for now)
bars = ax.bar(x, y, width=0.6, alpha=0)
Replace with rounded patches
for bar in bars: bbox = FancyBboxPatch( (bar.get_x(), 0), bar.get_width(), bar.get_height(), boxstyle="round,pad=0.02,rounding_size=0.15", # adjust rounding linewidth=0, facecolor="skyblue" ) ax.add_patch(bbox)
plt.show()
This gives bars with rounded corners.
-
Clipping with bar_container.patches You can loop over ax.patches (the bar rectangles) and set their joinstyle or clip them with a rounded Path.