matplotlib とは

Python の画像処理ライブラリ。

anatomy

Usage Guide より)

使い方

$ pip install matplotlib

種々の描画

折れ線

from matplotlib import pyplot as plt
import numpy as np

x = np.arange(0, 1.4, 0.01)
y1 = x ** 2
y2 = x ** 3

# グラフのタイトル
plt.title('The Title')
# X軸, Y軸のラベル
plt.xlabel('value of $x$')
plt.ylabel('value of $y$')
# X軸, Y軸の描画範囲
plt.xlim(0, 2.0)
plt.ylim(0, 3.5)
# 目盛りの設定
plt.xticks([0.1, 0.2, 0.4, 0.8, 1.6])
plt.yticks(np.arange(0, 3.0, 0.15))
# 線の色, 線の太さ, 凡例
plt.plot(x, y1, color='blue', linewidth=1.0, label='$x^2$')
plt.plot(x, y2, color='red',  linewidth=2.0, label='$x^3$')
# 凡例を表示させる
plt.legend()
# グリッドを表示させる
plt.grid()

plt.show()

散布図

from matplotlib import pyplot as plt
import numpy as np

N = 1000
r = np.random.rand(N)
theta = np.random.rand(N) * np.pi * 2
plt.scatter(r*np.cos(theta), r*np.sin(theta), s=2.0, label='foo')
plt.legend()
plt.show()

ヒストグラム

from matplotlib import pyplot as plt
import numpy as np

N = 1000
x = np.random.randn(N)
plt.hist(x, bins=20, label='hoge')
plt.legend()
plt.show()

塗りつぶし

from matplotlib import pyplot as plt
import numpy as np

x = np.arange(-4.0, 4.0, 0.01)
y1 = np.sin(x+1)
y2 = np.sin(x)
y3 = 0.2 * x

plt.plot(x, y1, color='black')
plt.plot(x, y2, color='black')
plt.plot(x, y3, color='black')
plt.fill_between(x, y1, y2, facecolor='red', alpha=0.3, label='foo')
plt.fill_between(x, y2, y3, facecolor='red', alpha=0.7, label='bar', where=(x>0)&(y3<y2))
plt.grid()
plt.show()

plt.plot(y1, x, color='black')
plt.plot(y2, x, color='black')
plt.plot(y3, x, color='black')
plt.fill_betweenx(x, y1, y2, facecolor='red', alpha=0.3, label='foo')
plt.fill_betweenx(x, y2, y3, facecolor='red', alpha=0.7, label='bar', where=(x>0)&(y3<y2))
plt.legend()
plt.grid()
plt.show()

等高線

from matplotlib import pyplot as plt
import numpy as np

x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)
x, y = np.meshgrid(x, y)
z = 2*np.exp(-((x+3)**2+y**2)/2) - np.exp(-((x-2)**2+(y-2)**2)/2) - 2*np.exp(-((x-2)**2+(y+2)**2)/2)

plt.contour(x, y, z, 20)
plt.show()

cs = plt.contour(x, y, z, 20)
plt.clabel(cs, inline=1, fontsize=10)
plt.show()

plt.contourf(x, y, z, 20, cmap='gray')
plt.show()

円グラフ

from matplotlib import pyplot as plt

labels = ['A', 'B', 'C', 'D', 'E']
sizes = [5, 4, 3, 2, 1]
colors = ['r', 'g', 'b', 'm', 'c']

plt.pie(sizes, labels=labels, colors=colors, autopct='%1.2f%%')
plt.show()

plt.pie(sizes, radius=1, labels=labels, colors=colors, autopct='%1.2f%%', wedgeprops=dict(width=0.3, edgecolor='w'))
plt.show()

表付きグラフ

from matplotlib import pyplot as plt
import numpy as np

values = [[1,2,3,4],[5,6,7,8]]
collabels = ['A', 'B', 'C', 'D']
rowlabels = ['a', 'b']
plt.plot(np.arange(0, 10, 0.1), np.arange(0, 10, 0.1))
plt.table(cellText=values, rowLabels=rowlabels, colLabels=collabels, loc='top')
plt.show()

グラフの配置

Tips

線・点のスタイル

線のスタイル:

from matplotlib import pyplot as plt
import numpy as np

x = np.arange(0, 10.0+1, 1.0)
y = x * 0.2
linestyles = [
    'solid', 'dashed', 'dotted', 'dashdot',
    (0, (1, 10)),
    (0, (5, 10)),
    (0, (5, 1)),
    (0, (3, 10, 1, 10)),
    (0, (3, 1, 1, 1)),
    (0, (3, 5, 1, 5, 1, 5)),
    (0, (3, 10, 1, 10, 1, 10)),
    (0, (3, 1, 1, 1, 1, 1))
]
for style in linestyles:
    plt.plot(x, y, linestyle=style, label='{}'.format(style))
    y += 1.0
plt.legend(ncol=1, bbox_to_anchor=(1.05, 0.93, 0.5, .100))
plt.show()

点のスタイル:

from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
import numpy as np

x, y = np.array([0]), np.array([0])
cnt = 0
for k, v in Line2D.markers.items():
    if cnt%10 == 0:
        x = 0
        y += 1
    x += 1
    plt.scatter(x, y, marker=k, label='{} {}'.format(k, v))
    cnt += 1
plt.legend(ncol=3, bbox_to_anchor=(1.05, 0.93, 0.5, .100))
plt.show()

凡例の位置調整