matplotlib:饼图中的重叠标签

发布于 2025-01-19 00:54:44 字数 769 浏览 2 评论 0原文

我必须为以下数据制作饼图:

在此处输入图像描述

但是,由于较大的数字有数百个,而较小的数字小于 1,因此图形的标签最终会因重叠而难以辨认。例如,这是新加坡的图表:

在此处输入图像描述

我尝试过减小字体大小并增大图形大小,但由于重叠太多,这样做根本没有帮助。以下是我的图表所需的代码:

import matplotlib.pyplot as plt
plt.pie(consumption["Singapore"], labels = consumption.index)
fig = plt.gcf()
fig.set_size_inches(8,8)
ax = plt.gca()
handles, labels = ax.get_legend_handles_labels()
labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0], reverse=True))
plt.show()

有什么方法可以解决这个问题吗?

I have to make a piechart for the following data:

enter image description here

However, because the larger numbers are in the hundreds while the smaller numbers are lesser than 1, the labels for the graph end up illegible due to overlapping. For example, this is the graph for Singapore:

enter image description here

I have tried decreasing the font size and increasing the graph size but because it overlaps so much, doing so doesn't really help at all. Here are the necessary codes for my graph:

import matplotlib.pyplot as plt
plt.pie(consumption["Singapore"], labels = consumption.index)
fig = plt.gcf()
fig.set_size_inches(8,8)
ax = plt.gca()
handles, labels = ax.get_legend_handles_labels()
labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0], reverse=True))
plt.show()

Is there any way to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2025-01-26 00:54:44

重叠标签字符的问题无法通过编程完全解决。如果您只应对挑战,请首先将它们分组以汇总标签数量。分组的数据帧针对饼图。但是,它仍然重叠,因此获得当前标签位置并更改重叠标签的位置。

new_df = consumption.groupby('Singapore')['Entity'].apply(list).reset_index()
new_df['Entity'] = new_df['Entity'].apply(lambda x: ','.join(x)) 
new_df

    Singapore   Entity
0   0.000000    Biofuels,Wind,Hydro,Nuclear
1   0.679398    Other
2   0.728067    Solar
3   5.463305    Coal
4   125.983605  Gas
5   815.027694  Oil

import matplotlib.pyplot as plt

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

wedges, texts = ax.pie(new_df["Singapore"], wedgeprops=dict(width=0.5), startangle=0, labels=new_df.Entity)
# print(wedges, texts)

texts[0].set_position((1.1,0.0))
texts[1].set_position((1.95,0.0))
texts[2].set_position((2.15,0.0))

plt.legend()

plt.show()

The problem of overlapping label characters cannot be completely solved by programming. If you're dealing with your challenges only, first group them to aggregate the number of labels. The grouped data frames are targeted for the pie chart. However, it still overlaps, so get the current label position and change the position of the overlapping label.

new_df = consumption.groupby('Singapore')['Entity'].apply(list).reset_index()
new_df['Entity'] = new_df['Entity'].apply(lambda x: ','.join(x)) 
new_df

    Singapore   Entity
0   0.000000    Biofuels,Wind,Hydro,Nuclear
1   0.679398    Other
2   0.728067    Solar
3   5.463305    Coal
4   125.983605  Gas
5   815.027694  Oil

import matplotlib.pyplot as plt

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

wedges, texts = ax.pie(new_df["Singapore"], wedgeprops=dict(width=0.5), startangle=0, labels=new_df.Entity)
# print(wedges, texts)

texts[0].set_position((1.1,0.0))
texts[1].set_position((1.95,0.0))
texts[2].set_position((2.15,0.0))

plt.legend()

plt.show()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文