2019年7月24日水曜日

ガウス関数曲線のベクトル画像

任意のガウス関数(正規分布)の曲線は,標準正規分布を水平,垂直方向に拡大・縮小することで得られる. パワーポイント等の発表資料に利用でしやすいようにPython(Jupyter Notebook)でベクトル画像を作成したので掲載しておく.

ここからSVG画像をダウンロードできる.
https://github.com/r168xr169/plot_normal_curve

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

# 平均と標準偏差と定義域
mu = 0.0
sigma = 1.0
x_min = -4
x_max = 4

# ガウス関数のデータ作成
x = np.arange(x_min, x_max, 0.001)
y = norm.pdf(x)

# build the plot
fig, ax = plt.subplots(figsize=(9,6))
plt.style.use('fivethirtyeight')
ax.plot(x,y, color='black')

# グリッドと軸非表示
plt.axis('off')
ax.grid(False)

# 平均,標準偏差の線
plt.hlines(norm.pdf(sigma), 0, 1, "red", linestyles='dashed')
plt.vlines(0, 0, norm.pdf(mu), "blue", linestyles='dashed')

# x軸
plt.hlines(0, x_min, x_max, "green")

# SVG画像の保存
plt.savefig('normal_curve.svg',format="svg",dpi=600,bbox_inches='tight',transparent=True)
plt.show()