技術をかじる猫

適当に気になった技術や言語、思ったこと考えた事など。

Matplotlib の使い方近辺

# Matplotlib と seaborn (ヘルパー)
import matplotlib as mpl
import seaborn as sns

# pyplot
import matplotlib.pyplot as plt

%matplotlib inline

散布図

# ライブラリ
import numpy as np
import numpy.random as random

random.seed(0)

# x,y
x = np.random.randn(30)
y = np.sin(x) + np.random.randn(30)

# グラフ保大きさ指定
plt.figure(figsize=(10, 6))

# 散布図1
plt.plot(x, y, 'o')

f:id:white-azalea:20190821002604p:plain

# 散布図2
plt.figure(figsize=(10, 6))
plt.scatter(x, y)

# 後付けパラメータ
plt.title('Set title')
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)

f:id:white-azalea:20190821002608p:plain

連続値

np.random.seed(0)

np_x           = np.arange(1000)
np_y_rand = np.random.randn(1000).cumsum()

plt.figure(figsize=(10, 6))
plt.plot(np_x, np_y_rand, label='Label')
plt.legend()

plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)

f:id:white-azalea:20190821002612p:plain

グラフ分割

plt.figure(figsize=(20, 6))

# 1 行 2 列のうち 1 カラム目
plt.subplot(1, 2, 1)
x1 = np.linspace(-10, 10, 100)
plt.plot(x1, np.sin(x1))

# 1 行 2 列の 2 カラム目
plt.subplot(1, 2, 2)
x2 = np.linspace(-10, 10, 100)
plt.plot(x2, np.sin(x2 * 2))

plt.grid(True)

f:id:white-azalea:20190821002615p:plain

関数グラフ

def example_func(x):
    return x**2 + 2*x + 1

x = np.arange(-10, 10)
plt.figure(figsize=(20, 5))
plt.plot(x, example_func(x))
plt.grid(True)

f:id:white-azalea:20190821002618p:plain

ヒストグラム

random.seed(0)

plt.figure(figsize=(20, 6))

# Histgram
plt.hist(np.random.randn(10 ** 5) * 10 + 50, bins = 60, range = (20, 80))
plt.grid(True)

f:id:white-azalea:20190821002621p:plain

# ヘルプ表示(help 関数と一緒かも?)
?plt.hist

演習問題

# 2-10: y = 5x + 3 関数を -10, 10 の範囲で作画
def example_func(x):
    return 5 * x + 3

x = np.linspace(-10, 10, 100)

plt.figure(figsize=(10, 5))
plt.plot(x, example_func(x))

f:id:white-azalea:20190821002625p:plain

# 2-11: sin,cos グラフを重ねて表示.
plt.figure(figsize=(10, 6))

plt.plot(x, np.sin(x), label = 'Sin')
plt.plot(x, np.cos(x), label = 'Cos')
plt.legend()

# 重ねるってこうですか?

f:id:white-azalea:20190821002630p:plain

plt.figure(figsize=(20, 6))

plt.subplot(1, 2, 1)
plt.plot(x, np.sin(x))

plt.subplot(1, 2, 2)
plt.plot(x, np.cos(x))
# それともこうですか?

f:id:white-azalea:20190821002633p:plain

# 2-12: 0-1 の 1000 乱数を 2 組作成してヒストグラムグラフ2個作画。
x1 = np.random.uniform(0.0, 1.0, 1000)
x2 = np.random.uniform(0.0, 1.0, 1000)

plt.figure(figsize=(20, 6))
plt.subplot(1, 2, 1)
plt.hist(x1)

plt.subplot(1, 2, 2)
plt.hist(x2)

f:id:white-azalea:20190821002638p:plain

その他

# この x, y の組み合わせで半径 1 の円に入る座標は何点ある?
np.random.seed(0)
N = 10000
x = np.random.uniform(0.0, 1.0, N)
y = np.random.uniform(0.0, 1.0, N)

import math
cnt = 0
inside_x = []
inside_y = []
outside_x = []
outside_y = []
for n in range(N):
    m = math.hypot(x[n], y[n])
    if m <= 1.0:
        cnt = cnt + 1
        inside_x.append(x[n])
        inside_y.append(y[n])
    else:
        outside_x.append(x[n])
        outside_y.append(y[n])

cnt
7868
# 図にしてみる
plt.figure(figsize=(10, 10))
plt.scatter(inside_x, inside_y, color = 'g')
plt.scatter(outside_x, outside_y, color = 'r')

f:id:white-azalea:20190821002641p:plain

print(f'円周率の近似(モンテルカルロ法): { 4.0 * cnt / N }')
円周率の近似(モンテルカルロ法): 3.1472