How to avoid overlapping wedge drop labels on a Matplotlib donut chart?

Viewed 147

I have the following donut chart:

As you can see in the picture, when the percentage of the wedge drops is smaller than 3% the percentage tags start to overlap with their neighbours.

Question:

How can this issue be mitigated or avoided while keeping the percentage tags on the chart?

enter image description here

Minimal Reproducible Example:

import pandas as pd
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10, 10))

data = {"Category":["Housing","Groceries","Subscriptions","Rectreation","Other","Health","Transport"],
"Amount": [918.39, 546.20, 289.62, 60.59, 39.74, 19.99, 19.60]}

colors = ["#67b7dc", "#6794dc", "#6771dc" , "#8067dc" , "#a367dc","#67b7dc","#6794dc","#6771dc"]
df = pd.DataFrame(data)

ax.pie(df["Amount"],
            labels=df["Category"],
            autopct='%1.1f%%',
            wedgeprops=dict(edgecolor=  "white", linewidth= 1.5, width=0.3),
            startangle=270,
            colors = colors,
            pctdistance=0.85,
            labeldistance=1.2,
            textprops={'color':"w", 'fontsize': 14} )

ax.axis('equal') 
ax.legend(loc = 'center', prop={'size': 15})

plt.show()
1 Answers

Well seems like there is no magical way to avoid this when you're dealing with a static image. I ended up using Plotly because it allows you to hover over the small wedges and view the percentage even if the percentage is too small to display.

enter image description here

Related