gpt4 book ai didi

python - 如何避免 Matplotlib 圆环图上重叠的楔形下降标签?

转载 作者:行者123 更新时间:2023-12-05 06:54:54 26 4
gpt4 key购买 nike

我有以下圆环图:

正如您在图片中看到的,当楔形下降的百分比小于 3% 时,百分比标签开始与它们的邻居重叠。

问题:

如何在保持图表上的百分比标记的同时缓解或避免此问题?

enter image description here

最小可重现示例:

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()

最佳答案

当您处理静态图像时,似乎没有什么神奇的方法可以避免这种情况。我最终使用了 Plotly因为它允许您将鼠标悬停在小楔子上并查看百分比,即使百分比太小而无法显示。

enter image description here

关于python - 如何避免 Matplotlib 圆环图上重叠的楔形下降标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65438944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com