Matplotlib-pie-chart
提供:Dev Guides
Matplotlib-円グラフ
円グラフで表示できるデータは1つだけです。 円グラフは、アイテムの合計に比例して、1つのデータ系列内のアイテム(ウェッジと呼ばれる)のサイズを示します。 円グラフのデータポイントは、円全体の割合として表示されます。
Matplotlib APIには、配列内のデータを表す円グラフを生成する* pie()関数があります。 各ウェッジの小数領域は x/sum(x)*で与えられます。 sum(x)<1の場合、xの値は小数領域を直接与え、配列は正規化されません。 結果のパイには、サイズ1-sum(x)の空のくさびがあります。
円グラフは、図と軸が正方形の場合、または軸のアスペクトが等しい場合に最適に見えます。
パラメーター
次の表は、円グラフのパラメータを示しています-
x | array-like. The wedge sizes. |
labels | list. A sequence of strings providing the labels for each wedge. |
Colors | A sequence of matplotlibcolorargs through which the pie chart will cycle. If None, will use the colors in the currently active cycle. |
Autopct | string, used to label the wedges with their numeric value. The label will be placed inside the wedge. The format string will be fmt%pct. |
次のコードは、pie()関数を使用して、さまざまなコンピューター言語コースに登録している学生のリストの円グラフを表示します。 割合の割合は、%1.2f%に設定された autopct パラメーターを使用して、それぞれのウェッジ内に表示されます。
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.axis('equal')
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
ax.pie(students, labels = langs,autopct='%1.2f%%')
plt.show()