简体   繁体   English

改进子图中饼图上的标签

[英]Improve labeling on pie chart in subplot

I'm plotting a figure with three subplots: 我正在绘制一个包含三个子图的图:

fig = plt.figure(fignum)
ax11 = plt.subplot2grid((2,2), (0,0))
ax12 = plt.subplot2grid((2,2), (0,1))
ax13 = plt.subplot2grid((2,2), (1,0), colspan=2)
ax13.axis('off')

I'm using Pandas for my data and I want to plot a bar chart, a pie chart, and a table. 我正在使用Pandas作为我的数据,我想绘制条形图,饼图和表格。 I'm doing this: 我这样做:

d = pd.Series(data,index)

#plotting the first subplot
d.plot(kind='bar',ax=ax11)

#plotting the second one and adjusting size of labels and percentages
(wedges, texts, autotexts) = ax12.pie(d,labels=d.index,autopct='%1.1f%%')
ax12.axis('equal')
for t in texts:
        t.set_size(7)
for t in autotexts:
        t.set_size(7)

#plotting the third axes, the table
ax13.table(cellText=table,colLabels=columns,loc='bottom')

This is how it turns out: 结果如下:

在此输入图像描述

How can I make the pie chart look better by not having the labels overlap? 如何通过不重叠标签使饼图看起来更好? I've tried adding a legend but it covers up the chart and I don't even know if it's ending up in the location I'm specifying: 我尝试添加一个图例,但它覆盖了图表,我甚至不知道它是否在我指定的位置结束:

ax12.legend(index,loc='lower right',fontsize=7)

在此输入图像描述

Is there a way to move the legend down into that empty space under the pie chart? 有没有办法将图例向下移动到饼图下的空白区域? I'll remove the labels from the pie chart itself once the legend looks good. 一旦图例看起来不错,我将从饼图中删除标签。

bbox_to_anchor lets you position the legend on the figure's canvas. bbox_to_anchor允许您将图例放置在图形的画布上。

see: http://matplotlib.org/users/legend_guide.html 请参阅: http//matplotlib.org/users/legend_guide.html

import matplotlib.pyplot as plt
import pandas 

fig = plt.figure()
ax11 = plt.subplot2grid((2,2), (0,0))
ax11.bar([1,2,3,4,3,2,1],[1,2,3,4,3,2,1])
ax11.set_xticklabels(["local user","whetever works","L'ours tourne","en rond","dans sa cage",2,1], rotation="vertical")

ax12 = plt.subplot2grid((2,2), (0,1))
ax12.pie( [10,20,30,5,5,6,4,10,10], labels = ["bewarfefvl","easdfgvagfvd","asasdfve.sd","rgdegaf","adfewga","qargw","qaerghrttw","errrrd","ejjjjd"],autopct='%1.1f%%')
ax12.axis('equal')
ax12.legend(loc='lower right',fontsize=7, bbox_to_anchor = (0.75, -01.0) )


ax13 = plt.subplot2grid((2,2), (1,0), colspan=2)
ax13.axis('off')

plt.show()

Although you could skip the legend and improve the readability of your pie-chart by adjusting it. 虽然您可以通过调整它来跳过图例并提高饼图的可读性。 I can think of three ways of doing it but it is by no mean an exhaustive list: 我可以想到三种方法,但它绝不是一个详尽的清单:

  1. When passing a list as argument, you can influence the relative position of each slice in the chart, in order to avoid clusters of small slices, by swapping the order of values in the list. 将列表作为参数传递时,可以通过交换列表中值的顺序来影响图表中每个切片的相对位置,以避免小切片簇。 There is likely a way to achieve this in pandas . pandas有可能实现这一目标。

  2. labeldistance lets you control the distance of label to the chart. labeldistance允许您控制标签到图表的距离。

  3. Passing a second array as explode argument, lets you offset each slice's position. 将第二个数组作为explode参数传递,可以使每个切片的位置偏移。

A simple example below: 一个简单的例子如下:

ax12 = plt.subplot2grid((2,2), (0,1))
labels = ["bewarfefvl","easdfgvagfvd","asasdfve.sd",
              "rgdegaf","adfewga","qargw","qaerghrttw","errrrd","ejjjjd"]
ax12.pie( [10,20,30,5,5,6,4,10,10], [0,0,0.0,0.1,0.3,.5,0,0,0], 
                          labels = labels, labeldistance=1.2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM