可視化

Matplotlib

一般例

import matplotlib.pyplot as plt

import numpy as np

%matplotlib inline

x = np.linspace(0, 2 * np.pi)

y1 = np.sin(x)

y2 = np.cos(x)

plt.title('Graph')

plt.xlabel('x-axis')

plt.ylabel('y-axis')

plt.grid(True)

positions = [0, np.pi/2, np.pi, np.pi * 3/2, np.pi * 2]

labels = ['0', '90', '180', '270', '360']

plt.xticks(positions, labels)

plt.figure(figsize=(5, 5))

plt.plot(x, y1, color='b', label='y=sin(x)')

plt.plot(x, y2, color='k', label='y=cos(x)')

plt.legend(['y=sin(x)', 'y=cos(x)'])

plt.show()

折れ線グラフ

days = np.arange(1, 11)

weight = np.array([10, 20, 18 ...])

plt.ylim([0, weight.max() + 1])

plt.xlabel('days')

plt.ylabel('weight')

plt.plot(days, weight, linestyle='--', marker='o', markerfacecolor='k')

plt.show()

棒グラフ

x = [...]

y1 = [...]

y2 = [...]

labels = [...]

plt.bar(x, y1, tick_label=labels)

plt.bar(x, y2, bottom=y1)

plt.legend(['y1', 'y2'])

plt.show()

ヒストグラム

np.random.seed(0)

data = np.random.randn(10000)

plt.hist(data, bins=100, normed=True)

# plt.hist(data, bins=100, normed=True, cumulative=True)

plt.show()

散布図

np.random.seed(0)

x = np.random.choice(np.arange(100), 100)

y = np.random.choice(np.arange(100), 100)

z = np.random.choice(np.arange(100), 100)

plt.scatter(x, y, marker='s', color='k')

# plt.scatter(x, y, s=z) # 値に応じて大きさ

# plt.scatter(x, y, c=z, cmap='Blues') # 値に応じて濃さ

# plt.colorbar()

plt.show()

円グラフ

data = [...]

labels = [...]

explode = [0, 0, 0.1, ...]

plt.pie(data, labels=labels, explode=explode)

plt.axis('equal')

plt.show()

Seaborn

import seaborn as sns

sns.distplot(data['y'], bins=50)

sns.pairplot(data) # 相関関係