コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%matplotlib inline | |
import matplotlib.pyplot as plt | |
import numpy as np | |
fig, ax = plt.subplots() | |
vals = np.array([[30., 20.], [10., 20.], [45., 30.], [45., 30.]]) | |
cmap = plt.get_cmap("tab20b") | |
outer_colors = cmap(np.arange(4)*4) | |
inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10, 13,14])) | |
size = 0.25 | |
ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors, | |
wedgeprops=dict(width=size, edgecolor='w')) | |
ax.pie(vals.flatten(), radius=1-size, colors=inner_colors, | |
wedgeprops=dict(width=size, edgecolor='w')) | |
#ax.set(aspect="equal") | |
#plt.savefig("donuts_pie.png",bbox_inches = 'tight', pad_inches = 0) | |
plt.show() |

解説
はじめに
ax.pie()は円グラフを作成するメソッドだが、 円ではなくドーナツ状グラフもpie()メソッドで作成できる。 ここでは、2層構造をもつデータを、内側&外側のドーナツ状グラフで表示した例を説明する。 ドーナツ状のグラフを作成するには、wedgepropsの項目でwidthを設定すればいい。
モジュールのインポート
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
データの生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vals = np.array([[30., 20.], [10., 20.], [45., 30.], [45., 30.]])
(4, 2)の形状のてきとうなデータを生成。
色の設定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmap = plt.get_cmap("tab20b")
outer_colors = cmap(np.arange(4)*4)
inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10, 13,14]))
tab20bは、
color example code: colormaps_reference.py — Matplotlib 2.0.2 documentation
のQualitative colormapsにあり、同系色の色が4個ずつ連なったカラーマップとなっている。そのうち、外側のドーナツの色を0から4こ飛ばしで選択し、内側のドーナツの色を1,2個目、5, 6個目, 9, 10個目, 13,14個目と選択した。
ドーナツグラフの表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
size = 0.25
ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
wedgeprops=dict(width=size, edgecolor='w'))
ax.pie(vals.flatten(), radius=1-size, colors=inner_colors,
wedgeprops=dict(width=size, edgecolor='w'))
vals.sum(axis=1)でnp.array全体ではなく、array内のarrayの合計を求めることができるので、4個のデータが得られる。
vals.flatten()は、np.arrayを1次元に展開するので、8個のデータが得られる。
colorsは、各要素に設定した各色が表示されることとなる。
sizeを0.25とし、width=sizeとすることでドーナツグラフとしている。
外側のグラフの半径を1とし、内側のグラフの半径を1-sizeとすることで、2層構造をうまく配置している。
参考
https://matplotlib.org/gallery/pie_and_polar_charts/nested_pie.html
matplotlib.axes.Axes.pie — Matplotlib 3.10.1 documentation
コメント