CodexBloom - Programming Q&A Platform

best practices for overlapping labels in a Matplotlib pie chart with percentage annotations?

๐Ÿ‘€ Views: 60 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-13
matplotlib data-visualization charts Python

I'm sure I'm missing something obvious here, but I'm working on a project and hit a roadblock. I'm trying to create a pie chart using Matplotlib (version 3.6.2) where I want to display both the labels and the percentage of each slice. However, I'm running into issues with overlapping labels, making them hard to read. Hereโ€™s the code Iโ€™m using: ```python import matplotlib.pyplot as plt import numpy as np sizes = [15, 30, 45, 10] labels = ['A', 'B', 'C', 'D'] fig1, ax1 = plt.subplots() ax1.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90) ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. plt.show() ``` While this works fine, the labels for smaller slices overlap with each other and are not easily readable. Iโ€™ve tried adjusting the `autopct` parameter and even increased the figure size, but the overlapping continues. I also attempted to use the `labeldistance` parameter to reposition labels, but that didnโ€™t resolve the scenario. Does anyone have suggestions on how to avoid these overlapping labels or reposition them more effectively? I would appreciate any best practices for improving the clarity of the pie chart in this context. My development environment is Linux. Is there a better approach? Hoping someone can shed some light on this.