【matplotlib】円グラフ(デザイン)[Python]

  • URLをコピーしました!
目次

Matplotlibで円グラフを表示

前回、Matplotlibを使って円グラフを表示するための基本とラベルの表示の仕方を解説しました。

今回はさらに円グラフのデザインの変更の仕方を解説していきたいと思います。

ということでまずは基本の形から。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x)

実行結果
([<matplotlib.patches.Wedge at 0x7fa60d142f10>,
  <matplotlib.patches.Wedge at 0x7fa60d152250>,
  <matplotlib.patches.Wedge at 0x7fa60d1525d0>,
  <matplotlib.patches.Wedge at 0x7fa60d152b90>,
  <matplotlib.patches.Wedge at 0x7fa60d15a810>],
 [Text(1.0864571742518743, 0.1720779140872905, ''),
  Text(0.8899186877588753, 0.6465637858537406, ''),
  Text(0.1720778759417422, 1.086457180293535, ''),
  Text(-0.9801072023231266, 0.4993894992431598, ''),
  Text(0.17207792680247347, -1.086457172237987, '')])

円グラフを離して表示:explode

これまで円グラフは各グラフ領域がくっついて、まん丸な円となって表示されていました。

しかし時にはどれかのグラフ領域や各グラフ領域を目立たせたいなんて時があると思います。

その時、グラフ領域を離して表示することができるオプションが「explode」です。

explodeの値はリストとしてそれぞれのグラフ領域の位置を指定します。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, explode=[0.1, 0.1, 0.1, 0.1, 0.1])

実行結果
([<matplotlib.patches.Wedge at 0x7fa60d1e66d0>,
  <matplotlib.patches.Wedge at 0x7fa60d1e69d0>,
  <matplotlib.patches.Wedge at 0x7fa60d1e6d50>,
  <matplotlib.patches.Wedge at 0x7fa60d1f1350>,
  <matplotlib.patches.Wedge at 0x7fa60d1f1f90>],
 [Text(1.1852260082747719, 0.18772136082249874, ''),
  Text(0.9708203866460458, 0.7053423118404443, ''),
  Text(0.18772131920917332, 1.1852260148656746, ''),
  Text(-1.0692078570797745, 0.5447885446289016, ''),
  Text(0.18772137469360742, -1.185226006077804, '')])

上の例では全部同じ比率で離していますが、注目させたいところだけさらに離すということも可能です。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, explode=[1, 0.1, 0.1, 0.1, 0.1])

実行結果
([<matplotlib.patches.Wedge at 0x7fa60d38bbd0>,
  <matplotlib.patches.Wedge at 0x7fa60d38bed0>,
  <matplotlib.patches.Wedge at 0x7fa60d399290>,
  <matplotlib.patches.Wedge at 0x7fa60d399850>,
  <matplotlib.patches.Wedge at 0x7fa60d3a24d0>],
 [Text(2.0741455144808505, 0.3285123814393728, ''),
  Text(0.9708203866460458, 0.7053423118404443, ''),
  Text(0.18772131920917332, 1.1852260148656746, ''),
  Text(-1.0692078570797745, 0.5447885446289016, ''),
  Text(0.18772137469360742, -1.185226006077804, '')])

ちなみにグラフ領域のリストの要素数とexplodeのリストの要素数が違うとエラーになります。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, explode=[0.2])

実行結果
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-103-748a16cbb4f6> in <module>
      7 x = [5, 10, 15, 25, 45]
      8 
----> 9 plt.pie(x, explode=[0.2])

/opt/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in pie(x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame, rotatelabels, data)
   2753         wedgeprops=wedgeprops, textprops=textprops, center=center,
   2754         frame=frame, rotatelabels=rotatelabels, **({"data": data} if
-> 2755         data is not None else {}))
   2756 
   2757 

/opt/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1563     def inner(ax, *args, data=None, **kwargs):
   1564         if data is None:
-> 1565             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1566 
   1567         bound = new_sig.bind(ax, *args, **kwargs)

/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in pie(self, x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame, rotatelabels)
   2929             raise ValueError("'label' must be of length 'x'")
   2930         if len(x) != len(explode):
-> 2931             raise ValueError("'explode' must be of length 'x'")
   2932         if colors is None:
   2933             get_next_color = self._get_patches_for_fill.get_next_color

ValueError: 'explode' must be of length 'x'

各グラフ領域の色を指定:colors

グラフ領域の色を指定する場合にはcolorsのオプションを用います。

その際、各グラフ領域の色を指定したリストを引数とします。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, colors=["red", "dimgray", "gray", "darkgray", "lightgray"])

実行結果
([<matplotlib.patches.Wedge at 0x7fa60d9292d0>,
  <matplotlib.patches.Wedge at 0x7fa60d9295d0>,
  <matplotlib.patches.Wedge at 0x7fa60d929950>,
  <matplotlib.patches.Wedge at 0x7fa60d929f10>,
  <matplotlib.patches.Wedge at 0x7fa60d934b90>],
 [Text(1.0864571742518743, 0.1720779140872905, ''),
  Text(0.8899186877588753, 0.6465637858537406, ''),
  Text(0.1720778759417422, 1.086457180293535, ''),
  Text(-0.9801072023231266, 0.4993894992431598, ''),
  Text(0.17207792680247347, -1.086457172237987, '')])

1箇所だけ赤になり、他のグラフ領域は灰色になりました。

ちなみにmatplotlibで指定できる色の名前リストはこちらです。

また色のリストの要素数がグラフ領域のリストの要素数と違っている場合、前から順番に使われていき、使い切ったら、また最初の色に戻ります。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, colors=["red", "blue", "green"])

実行結果
([<matplotlib.patches.Wedge at 0x7fa60efd3750>,
  <matplotlib.patches.Wedge at 0x7fa60efd3a50>,
  <matplotlib.patches.Wedge at 0x7fa60efd3dd0>,
  <matplotlib.patches.Wedge at 0x7fa60efd3590>,
  <matplotlib.patches.Wedge at 0x7fa60efde990>],
 [Text(1.0864571742518743, 0.1720779140872905, ''),
  Text(0.8899186877588753, 0.6465637858537406, ''),
  Text(0.1720778759417422, 1.086457180293535, ''),
  Text(-0.9801072023231266, 0.4993894992431598, ''),
  Text(0.17207792680247347, -1.086457172237987, '')])

影をつける:shadow

円グラフを立体的に見せるため、「shadow」のオプションを使って影をつけることができます。

選択肢としては影をつける(True)と影をつけない(False)があり、影をつけいないがデフォルトです。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, shadow=True)

実行結果
([<matplotlib.patches.Wedge at 0x7fa60da5ba90>,
  <matplotlib.patches.Wedge at 0x7fa60da5be50>,
  <matplotlib.patches.Wedge at 0x7fa60da67410>,
  <matplotlib.patches.Wedge at 0x7fa60da67cd0>,
  <matplotlib.patches.Wedge at 0x7fa60da72f90>],
 [Text(1.0864571742518743, 0.1720779140872905, ''),
  Text(0.8899186877588753, 0.6465637858537406, ''),
  Text(0.1720778759417422, 1.086457180293535, ''),
  Text(-0.9801072023231266, 0.4993894992431598, ''),
  Text(0.17207792680247347, -1.086457172237987, '')])

円の左下にわずかながら影ができているのが分かりますでしょうか?

explodeで各グラフ領域を離した場合、切り口にも影が付きます。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, explode=[0.1, 0.1, 0.1, 0.1, 0.1], shadow=True)

実行結果
([<matplotlib.patches.Wedge at 0x7fa60dac4390>,
  <matplotlib.patches.Wedge at 0x7fa60dac4750>,
  <matplotlib.patches.Wedge at 0x7fa60dac4cd0>,
  <matplotlib.patches.Wedge at 0x7fa60db145d0>,
  <matplotlib.patches.Wedge at 0x7fa60db20890>],
 [Text(1.1852260082747719, 0.18772136082249874, ''),
  Text(0.9708203866460458, 0.7053423118404443, ''),
  Text(0.18772131920917332, 1.1852260148656746, ''),
  Text(-1.0692078570797745, 0.5447885446289016, ''),
  Text(0.18772137469360742, -1.185226006077804, '')])

円グラフの開始の角度:startangle

円グラフの開始の角度を変更するには、「startagnle」を使います。

一番最初のグラフ領域を上にするにはこのstartangleを「90」と指定します。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, startangle=90)

実行結果
([<matplotlib.patches.Wedge at 0x7fa60dbac890>,
  <matplotlib.patches.Wedge at 0x7fa60dbacb90>,
  <matplotlib.patches.Wedge at 0x7fa60dbacf10>,
  <matplotlib.patches.Wedge at 0x7fa60dbb9510>,
  <matplotlib.patches.Wedge at 0x7fa60dbc4190>],
 [Text(-0.1720779140872905, 1.0864571742518743, ''),
  Text(-0.6465637858537403, 0.8899186877588755, ''),
  Text(-1.086457180293535, 0.1720778759417423, ''),
  Text(-0.49938949924316023, -0.9801072023231264, ''),
  Text(1.086457172237987, 0.17207792680247339, '')])

円グラフの半径を変更:radius

円グラフの半径を変更するには「radius」を使います。

通常の半径が1なので、半径を2倍にしたい場合はこのradiusを「2」とします。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, radius=2)

実行結果
([<matplotlib.patches.Wedge at 0x7fa60dc45e50>,
  <matplotlib.patches.Wedge at 0x7fa60dc52190>,
  <matplotlib.patches.Wedge at 0x7fa60dc52510>,
  <matplotlib.patches.Wedge at 0x7fa60dc52ad0>,
  <matplotlib.patches.Wedge at 0x7fa60dc5b750>],
 [Text(2.1729143485037485, 0.344155828174581, ''),
  Text(1.7798373755177506, 1.2931275717074813, ''),
  Text(0.3441557518834844, 2.17291436058707, ''),
  Text(-1.9602144046462533, 0.9987789984863196, ''),
  Text(0.34415585360494694, -2.172914344475974, '')])

グラフの並ぶ方向を変更:counterclock

グラフの並ぶ方向を変更するには「counterclock」を用います。

counterclockは英語で「反時計回り」で、このオプションのデフォルトは「True」となっています。

そのため、これまでのグラフは全部反時計回りに出力されていました。

時計回りに出力するには、「counterclock = False」とします。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, counterclock=False)

実行結果
([<matplotlib.patches.Wedge at 0x7fa60dd3c3d0>,
  <matplotlib.patches.Wedge at 0x7fa60dd3c6d0>,
  <matplotlib.patches.Wedge at 0x7fa60dd3ca50>,
  <matplotlib.patches.Wedge at 0x7fa60dd46050>,
  <matplotlib.patches.Wedge at 0x7fa60dd46c90>],
 [Text(1.0864571742518743, -0.1720779140872905, ''),
  Text(0.8899186877588753, -0.6465637858537406, ''),
  Text(0.1720778759417422, -1.086457180293535, ''),
  Text(-0.9801072023231266, -0.4993894992431598, ''),
  Text(0.17207792680247347, 1.086457172237987, '')])

グラフ領域の線の変更:wedgeprops

グラフ領域の囲線を変更するには、「wedgeprops」を使います。

指定の方法は前回紹介した「textprops」と似ています。

線の太さを「5」と色を「黒」と指定する場合は、「wedgeprops = {‘linewidth’: 5, ‘edgecolor’:”black”}」とします。

from matplotlib import pyplot as plt
%matplotlib inline

fig = plt.figure()
plt.clf()

x = [5, 10, 15, 25, 45]

plt.pie(x, wedgeprops = {'linewidth': 5, 'edgecolor':"black"})

実行結果
([<matplotlib.patches.Wedge at 0x7fa60ddd1b50>,
  <matplotlib.patches.Wedge at 0x7fa60ddd1e50>,
  <matplotlib.patches.Wedge at 0x7fa60dddd210>,
  <matplotlib.patches.Wedge at 0x7fa60dddd7d0>,
  <matplotlib.patches.Wedge at 0x7fa60dde9450>],
 [Text(1.0864571742518743, 0.1720779140872905, ''),
  Text(0.8899186877588753, 0.6465637858537406, ''),
  Text(0.1720778759417422, 1.086457180293535, ''),
  Text(-0.9801072023231266, 0.4993894992431598, ''),
  Text(0.17207792680247347, -1.086457172237987, '')])

線を付けると一気に雰囲気が変わるので、色々試してみるといいかもしれません。

残りのオプションとしては「center」と「frame」があるようなのですが、使ってみたところ、何が変わっているのか、使う場面があるのかよく分からなかったので解説は割愛します。

これで円グラフを色々と操れるようになったかと思います。

次回からはまた機械学習に戻り、新しいデータセットを試していきましょう。

ということで今回はこんな感じで。

よかったらシェアしてね!
  • URLをコピーしました!

コメント

コメントする

目次