matplotlib:饼图中的重叠标签
我必须为以下数据制作饼图:
但是,由于较大的数字有数百个,而较小的数字小于 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:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重叠标签字符的问题无法通过编程完全解决。如果您只应对挑战,请首先将它们分组以汇总标签数量。分组的数据帧针对饼图。但是,它仍然重叠,因此获得当前标签位置并更改重叠标签的位置。
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.