2
7

More than 5 years have passed since last update.

Pyplot(2) 円グラフ

Last updated at Posted at 2019-08-05

円グラフ

基本形

import matplotlib.pyplot as plt

sizes = [15, 30, 35, 10]

fig1, ax1 = plt.subplots()
ax1.pie(sizes)

plt.show()

pie_image_01.png

パラメータを指定しない場合、0度から半時計周りで描画される。
また、size(グラフの値)の合計が100ではない場合でも、比率で割合が算出される。

import matplotlib.pyplot as plt

# 合計で60の場合
sizes = [10, 20, 30]

fig1, ax1 = plt.subplots()
ax1.pie(sizes)

plt.show()

pie_image_02.png

ラベル表示

import matplotlib.pyplot as plt

labels = 'label1', 'label2', 'label3', 'label4'
sizes = [10, 20, 30, 40]

fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels)

plt.show()

pie_image_03.png

一部を円から切り離して表示

import matplotlib.pyplot as plt

labels = 'label1', 'label2', 'label3', 'label4'
sizes = [10, 20, 30, 40]
explode = (0, 0.1, 0, 0)

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels)

plt.show()

pie_image_04.png

explodeの値を大きくすることで、切り離す際の幅が大きくなる。

explode = (0.1, 0.3, 0, 0)

pie_image_05.png

90度位置から時計回りに描画

個人的に普段見慣れている円グラフの描画に合わせる。

![pie_image_11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/476387/de5f876a-d7dc-6c8d-366b-f03750001490.png)
import matplotlib.pyplot as plt

labels = 'label1', 'label2', 'label3', 'label4'
sizes = [10, 20, 30, 40]
explode = (0, 0.1, 0, 0)

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, counterclock=False, startangle=90)
plt.show()

pie_image_11.png

conterclockパラメータをFalseにすることで、時計回りの描画になる。
startangleパラメータで開始位置を調整する。

影をつける

import matplotlib.pyplot as plt

labels = 'label1', 'label2', 'label3', 'label4'
sizes = [10, 20, 30, 40]
explode = (0, 0.1, 0, 0)

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, shadow=True, counterclock=False, startangle=90)

plt.show()

pie_image_06.png

値をグラフ内に表示

import matplotlib.pyplot as plt

labels = 'label1', 'label2', 'label3', 'label4'
sizes = [10, 20, 30, 40]
explode = (0, 0.1, 0, 0)

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, shadow=True, autopct='%1.1f%%', counterclock=False, startangle=90)

plt.show()

pie_image_07.png

ドーナツ

import matplotlib.pyplot as plt

labels = 'label1', 'label2', 'label3', 'label4'
sizes = [10, 20, 30, 40]
explode = (0, 0.1, 0, 0)
size = 0.5

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, shadow=True, autopct='%1.1f%%', counterclock=False,wedgeprops=dict(width=size, edgecolor='w'), startangle=90)

plt.show()

pie_image_08.png

半径

import matplotlib.pyplot as plt

labels = 'label1', 'label2', 'label3', 'label4'
sizes = [10, 20, 30, 40]
explode = (0, 0.1, 0, 0)
size = 0.5

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, shadow=True, radius=2, autopct='%1.1f%%', counterclock=False,wedgeprops=dict(width=size, edgecolor='w'), startangle=90)

plt.show()

pie_image_09.png

radiusで半径を変更する。デフォルトは1。

入れ子の円グラフ

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

labels = 'label1', 'label2', 'label3'
size = 0.3
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])

cmap = plt.get_cmap("tab20c")
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10]))

ax.pie(vals.sum(axis=1), labels=labels, radius=1, colors=outer_colors, autopct='%1.1f%%', 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", title='Pie plot with `ax.pie`')

plt.show()

pie_image_10.png

円グラフを2つ描画している感じ。radiuswidthでサイズ調整して、入れ子に見えるようにしている。

2
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
7