簡體   English   中英

改進子圖中餅圖上的標簽

[英]Improve labeling on pie chart in subplot

我正在繪制一個包含三個子圖的圖:

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

我正在使用Pandas作為我的數據,我想繪制條形圖,餅圖和表格。 我這樣做:

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_to_anchor允許您將圖例放置在圖形的畫布上。

請參閱: 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()

雖然您可以通過調整它來跳過圖例並提高餅圖的可讀性。 我可以想到三種方法,但它絕不是一個詳盡的清單:

  1. 將列表作為參數傳遞時,可以通過交換列表中值的順序來影響圖表中每個切片的相對位置,以避免小切片簇。 pandas有可能實現這一目標。

  2. labeldistance允許您控制標簽到圖表的距離。

  3. 將第二個數組作為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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM