Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 改进子批次中饼图的标记_Python_Matplotlib_Plot_Charts_Pie Chart - Fatal编程技术网

Python 改进子批次中饼图的标记

Python 改进子批次中饼图的标记,python,matplotlib,plot,charts,pie-chart,Python,Matplotlib,Plot,Charts,Pie Chart,我正在绘制一个包含三个子图的图形: 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') 我使用熊猫作为我的数据,我想绘制一个条形图、一个饼图和一个表格。我正在这样做: d = pd.Series(data,index

我正在绘制一个包含三个子图的图形:

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')
我使用熊猫作为我的数据,我想绘制一个条形图、一个饼图和一个表格。我正在这样做:

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')
结果是这样的:

如何使饼图看起来更好,不让标签重叠?我尝试添加图例,但它掩盖了图表,我甚至不知道它是否在我指定的位置结束:

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


有没有办法将图例向下移动到饼图下的空白处?一旦图例看起来不错,我将从饼图本身删除标签。

bbox\u to\u anchor
用于将图例放置在图形画布上

见:

尽管您可以跳过图例并通过调整它来提高饼图的可读性。我可以想出三种方法,但这并不是一个详尽的清单:

  • 将列表作为参数传递时,可以通过交换列表中值的顺序来影响图表中每个切片的相对位置,以避免小切片的聚集。在
    pandas
    中可能有一种方法可以实现这一点

  • labeldistance
    用于控制标签到图表的距离

  • 将第二个数组作为
    explode
    参数传递,可以偏移每个切片的位置

  • 下面是一个简单的例子:

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