如何在matplotlib饼图中添加图例? [英] How to add a legend to matplotlib pie chart?

查看:639
本文介绍了如何在matplotlib饼图中添加图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此示例 http://matplotlib.org/examples/pie_and_polar_charts/pie_demo_features.html 一个> 如何在此饼图中添加图例? 我的问题是我有一个大片,占88.4%,第二大片是10.6%,另一个片是0.7和0.3%.饼图周围的标签不会出现(最大的切片除外),较小的切片也不会显示百分比值.因此,我想我可以添加一个显示名称和值的图例.但是我还不知道怎么...

Using this example http://matplotlib.org/examples/pie_and_polar_charts/pie_demo_features.html how could I add a legend to this pie chart? My problem is that I have One big slice 88.4%, the second largest slice is 10.6%, and the other slices are 0.7 and 0.3%. The labels around the pie don't appear (except for the biggest slice) and neither the percentage values for the smaller slices. So I guess I can add a legend showing the names and the values. But I haven't found out how...

# -*- coding: UTF-8 -*-
import matplotlib.pyplot as plt
# The slices will be ordered and plotted counter-clockwise.
labels = 'Rayos X', 'RMN en solución', 'Microscopía electrónica', 'Otros'
sizes = [88.4, 10.6, 0.7, 0.3]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
explode = (0.1, 0, 0, 0)
plt.pie(sizes, explode=explode, labels=labels, colors=colors, shadow=True, startangle=90)
plt.legend(title="técnica")
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.show()

推荐答案

我检查了您的代码,plt.legend()创建了一个图例,即您希望的样子.可能会设置loc="lower left",因此它不会与相关的饼图重叠.

I checked your code, and the plt.legend() creates a legend, just how you want it to be; maybe set the loc="lower left", so it does not overlap with the relevant pieces of pie.

对我来说,除了非标准字符外,字符串还可以正确显示-这可能会导致根本不显示字符串的问题.只有最大的片段和"Otros"不包含特殊字符.也许还尝试调整图形的大小,因为它们可能会从画布中推出.请参考如何使用matplotlib编写重音符号,然后使用正确的字符串重试.

For me, the strings are displayed properly, besides the non standard chars - which might cause the problem that they are not displayed to you at all. Only the biggest slice and "Otros" do not contain special chars. Maybe also try to resize the figure, as they might be pushed out of the canvas. Please refer to how to write accents with matplotlib and try again with proper strings.

不显示百分比,因为您没有设置要显示的百分比.请参考您发布的示例,因为您省略了autopct='%1.1f%%',它将绘制百分比.在这种特殊情况下,我不想绘制百分比,因为它们会像边界上的标签一样重叠,因为某些切片太小.也许将这些信息添加到图例中.

The percentages are not shown, because you did not set them to be shown. Please refer to the example posted by you, as you omitted autopct='%1.1f%%'which will plot the percentages. In this special case, I would rather not plot the percentages, as they will overlap just like the labels on the border, as some slices are too small. Maybe add these information to the legend.

将所有内容放在一起(除了特殊字符-我在激活TeX时遇到了一些问题),请尝试以下代码:

Putting it all together (besides the special chars - I had some problems activating TeX), try the following code:

# -*- coding: UTF-8 -*-
import matplotlib.pyplot as plt
# The slices will be ordered and plotted counter-clockwise.
labels = [r'Rayos X (88.4 %)', r'RMN en solucion (10.6 %)', 
r'Microscopia electronica (0.7 %)', r'Otros (0.3 %)']
sizes = [88.4, 10.6, 0.7, 0.3]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
patches, texts = plt.pie(sizes, colors=colors, startangle=90)
plt.legend(patches, labels, loc="best")
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.tight_layout()
plt.show()

这篇关于如何在matplotlib饼图中添加图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆