2017-08-18 686 views
3

円グラフプロットのラベル重複に関する多くの質問が掲載されました。しかし、私はそれらを凡例に変換する以外は自動化されたソリューションを見つけることができませんでした。私が多くの価値(約60)を持っているので、このソリューションは私のためには機能しません。また、凡例に変換すると、プロットが非常に乱雑で不明瞭になります。だから私の質問、パイの周りにパイのウェッジを付ける場合は、ラベルが良い間隔(重複なし)を持つことができるMatPlotLibバージョン2.0.2のための任意の自動化されたソリューションですか? 私が見つけた唯一の解決策は、注釈()で手動で行うことです。ダミーの値を持つスクリプトを下に見てください。また、パイのくさびを関連ラベルに矢印で結ぶことも可能ですか?MatPlotLib ver.2.0.2で円グラフのラベルが重なるのを避けるには?

Iは、Python 2.7とmatplotlibの使用2.0.2

おかげで、

例1(重複ラベル)(手動補正)

enter image description here

例2

enter image description here

import pylab 
import matplotlib.pyplot as plt 
fig, ax = plt.subplots() 
l = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],#[0, 0.1, 0, 0.1,0,0.1,0,0.1,0,0.1], 
      #labels=("one","two","three made up sentences","four is also ther","five becomes a sentence","six it is","seven long", "eight long sent", "nine, as bla bel mo","ten is also short"), 
      labels=("","","","","","six it is","seven long", "eight long sent", "nine, as bla bel mo","ten is also short"), 
      colors=("b","g","r","y", "b","g","r","y","g","black"), 
      startangle =20, 
      radius=1, 
      frame=True, # Plot axes frame with the chart if true. 
      labeldistance = 1.1) #returns a list of matplotlib.patches.Wedge objects 

l2 = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],#[0, 0.1, 0, 0.1,0,0.1,0,0.1,0,0.1], 
      colors=("r","g","b","w", "g","b","y","r","w","black"), 
      startangle =20, 
      radius=1-0.7, 
      frame=True) # Plot axes frame with the chart if true. 

coor = [t.get_position() for t in l[1]] 

ax.axis('equal') 
plt.annotate('one was very short now ext', xy= (coor[0][0], coor[0][1]))  # https://kite.com/docs/python/matplotlib.pyplot.annotate 
plt.annotate('two long sentense',   xy= (coor[1][0], coor[1][1])) 
plt.annotate('three things to say' ,  xy= (coor[2][0], coor[2][1]+0.02)) 
plt.annotate('four main tasks to do',  xy= (coor[3][0], coor[3][1]+0.04)) 
plt.annotate('five reasons to avoid',  xy= (coor[4][0], coor[4][1]+0.06)) 

plt.savefig('test_draft.pdf') 
plt.show() 
+0

はあなたがplotlyを見て持っていた、それはこのすべてを行うことができますか? [リンクはこちら](https://plot.ly/python/pie-charts/) –

+0

まず、matplotlibにテキストやラベルが重ならないように自動的にする方法はありません。矢印に関して、「注釈」は矢印を使用することを可能にする。たぶん[この質問](https://stackoverflow.com/questions/43349004/python-piechart-is-it-possible-to-do-callout-labels)もここに興味があります。 – ImportanceOfBeingErnest

答えて

2

テキストの回転を試すことができます。以下は、各くさびの角度を決定し、それに応じて注釈テキストを回転させます。テキストの配置を決定するために角度に応じていくつかの特別な調整が必要であり、またテキストが上下逆さまにならないようにする必要があります。これをさらに洗練する必要があるかもしれません。表示されます

import pylab 
import matplotlib.pyplot as plt 
import math 

fig, ax = plt.subplots() 

labels= [ 
    "one", 
    "two", 
    "three made up sentences", 
    "four is also there", 
    "five becomes a sentence", 
    "six it is", 
    "seven long", 
    "eight long sent", 
    "nine, as bla bel mo", 
    "ten is also short"] 

l = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2], 
      labels=[''] * len(labels), 
      colors=("b","g","r","y", "b","g","r","y","g","black"), 
      startangle=20, 
      radius=1, 
      frame=True, # Plot axes frame with the chart if true. 
      labeldistance=1.1) #returns a list of matplotlib.patches.Wedge objects 

l2 = ax.pie([1,0.2, 0.3,0.1,0.4,7,50,35,5,2],#[0, 0.1, 0, 0.1,0,0.1,0,0.1,0,0.1], 
      colors=("r","g","b","w", "g","b","y","r","w","black"), 
      startangle=20, 
      radius=1-0.7, 
      frame=True) # Plot axes frame with the chart if true. 

for label, t in zip(labels, l[1]): 
    x, y = t.get_position() 
    angle = int(math.degrees(math.atan2(y, x))) 
    ha = "left" 
    va = "bottom" 

    if angle > 90: 
     angle -= 180 

    if angle < 0: 
     va = "top" 

    if -45 <= angle <= 0: 
     ha = "right" 
     va = "bottom" 

    plt.annotate(label, xy=(x,y), rotation=angle, ha=ha, va=va, size=8) 

ax.axis('equal') 
plt.show() 

pie with rotated annotations

関連する問題