2013-01-05 17 views
7

matplotlib-demoに従って円グラフを作成しています:http://matplotlib.org/examples/pylab_examples/pie_demo.html
各ラベルの割合は自動ラベル付けされているようです。どのようにして、これらの自動ラベル付けされた相対値(%)を、円グラフにプロットしたものをfracs []の絶対値で置き換えることができますか?Matplotlib pie-chart:自動ラベル付き相対値を絶対値で置き換える方法

答えて

10

help(pie)は言う:

*autopct*: [ *None* | format string | format function ] 
    If not *None*, is a string or function used to label the 
    wedges with their numeric value. The label will be placed inside 
    the wedge. If it is a format string, the label will be ``fmt%pct``. 
    If it is a function, it will be called. 

ので、あなたはパイの合計サイズを掛けると100で割ることによって、元の値に変換する割合を変えることができます:

figure(1, figsize=(6,6)) 
ax = axes([0.1, 0.1, 0.8, 0.8]) 
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' 
fracs = [15, 30, 45, 10] 
total = sum(fracs) 
explode=(0, 0.05, 0, 0) 
pie(fracs, explode=explode, labels=labels, 
    autopct=lambda(p): '{:.0f}'.format(p * total/100), 
    shadow=True, startangle=90) 
show() 

+2

へパーセンテージと合計の両方を表示する:http://stackoverflow.com/questions/6170246/how-do-i-use-matplotlib-autopct – zehpunktbarron

+2

これは、そのコードで 'lambda p'でなければならず、括弧。 –

+0

@ K.-MichaelAye:括弧はPython 2.7で合法的でした。答えが書かれた日付を書き留めます。 –

関連する問題