Python에서 정규 분포를 그리는 방법: 예제 포함


Python에서 정규 분포를 그리려면 다음 구문을 사용할 수 있습니다.

 #x-axis ranges from -3 and 3 with .001 steps
x = np. arange (-3, 3, 0.001)

#plot normal distribution with mean 0 and standard deviation 1
plt. plot (x, norm. pdf (x, 0, 1))

x 배열은 x 축의 범위를 정의하고 plt.plot()은 지정된 평균 및 표준 편차를 사용하여 정규 분포 곡선을 생성합니다.

다음 예에서는 이러한 기능을 실제로 사용하는 방법을 보여줍니다.

예 1: 단일 정규 분포 도표화

다음 코드는 평균이 0이고 표준 편차가 1인 단일 정규 분포 곡선을 그리는 방법을 보여줍니다.

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

#x-axis ranges from -3 and 3 with .001 steps
x = np. arange (-3, 3, 0.001)

#plot normal distribution with mean 0 and standard deviation 1
plt. plot (x, norm. pdf (x, 0, 1)) 

Python의 정규 분포

차트에서 선의 색상과 너비를 변경할 수도 있습니다.

 plt. plot (x, norm. pdf (x, 0, 1), color=' red ', linewidth= 3 ) 

예 2: 다중 정규분포 도표화

다음 코드는 다양한 평균과 표준 편차를 사용하여 여러 정규 분포 곡선을 그리는 방법을 보여줍니다.

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

#x-axis ranges from -5 and 5 with .001 steps
x = np. arange (-5, 5, 0.001)

#define multiple normal distributions
plt. plot (x, norm. pdf (x, 0, 1), label=' μ: 0, σ: 1 ')
plt. plot (x, norm. pdf (x, 0, 1.5), label=' μ:0, σ: 1.5 ')
plt. plot (x, norm. pdf (x, 0, 2), label=' μ:0, σ: 2 ')

#add legend to plot
plt. legend () 

자유롭게 선 색상을 변경하고 제목과 축 레이블을 추가하여 차트를 완성하세요.

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

#x-axis ranges from -5 and 5 with .001 steps
x = np. arange (-5, 5, 0.001)

#define multiple normal distributions
plt. plot (x, norm. pdf (x, 0, 1), label=' μ: 0, σ: 1 ', color=' gold ')
plt. plot (x, norm. pdf (x, 0, 1.5), label=' μ:0, σ: 1.5 ', color=' red ')
plt. plot (x, norm. pdf (x, 0, 2), label=' μ:0, σ: 2 ', color=' pink ')

#add legend to plot
plt. legend (title=' Parameters ')

#add axes labels and a title
plt. ylabel (' Density ')
plt. xlabel (' x ')
plt. title (' Normal Distributions ', fontsize= 14 ) 

plt.plot() 함수에 대한 자세한 설명은 matplotlib 설명서를 참조하세요.

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다