Matplotlib 로짓 스케일 플로팅

Beginner

This tutorial is from open-source community. Access the source code

소개

이 랩에서는 Matplotlib 에서 로짓 축 (logit axes) 을 사용하여 플롯을 만드는 방법을 배웁니다. 로짓 축은 일반적으로 분포의 누적 분포 함수 (CDF, Cumulative Distribution Function) 를 나타내기 위해 확률 플롯에 사용됩니다. 이 랩에서는 math, numpy, 그리고 matplotlib.pyplot 라이브러리를 사용합니다.

VM 팁

VM 시작이 완료되면, 왼쪽 상단을 클릭하여 Notebook 탭으로 전환하여 실습을 위해 Jupyter Notebook에 접속하세요.

때로는 Jupyter Notebook 이 로딩을 완료하는 데 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한으로 인해 작업의 유효성 검사는 자동화될 수 없습니다.

학습 중 문제가 발생하면 Labby 에게 자유롭게 문의하십시오. 세션 후 피드백을 제공해주시면 문제를 신속하게 해결해 드리겠습니다.

필요한 라이브러리 가져오기 및 데이터 설정

math, numpy, 그리고 matplotlib.pyplot 라이브러리를 가져오고 플롯에 사용할 데이터를 설정합니다.

import math
import numpy as np
import matplotlib.pyplot as plt

xmax = 10
x = np.linspace(-xmax, xmax, 10000)
cdf_norm = [math.erf(w / np.sqrt(2)) / 2 + 1 / 2 for w in x]
cdf_laplacian = np.where(x < 0, 1 / 2 * np.exp(x), 1 - 1 / 2 * np.exp(-x))
cdf_cauchy = np.arctan(x) / np.pi + 1 / 2

로짓 스케일 (logit scale) 과 표준 표기법으로 플롯 생성

로짓 스케일과 표준 표기법을 사용하여 플롯을 생성합니다. 이는 set_yscale("logit")을 사용하여 y 축 스케일을 로짓으로 설정하고, set_ylim()을 사용하여 y 축 제한을 설정함으로써 수행할 수 있습니다. 또한 plot()을 사용하여 정규, 라플라시안, 코시 분포의 누적 분포 함수를 플롯하고, legend()를 사용하여 범례를 추가합니다.

fig, axs = plt.subplots(nrows=1, ncols=1, figsize=(6.4, 4.8))

axs.plot(x, cdf_norm, label=r"$\mathcal{N}$")
axs.plot(x, cdf_laplacian, label=r"$\mathcal{L}$")
axs.plot(x, cdf_cauchy, label="Cauchy")
axs.set_yscale("logit")
axs.set_ylim(1e-5, 1 - 1e-5)
axs.legend()
axs.grid()

plt.show()

로짓 스케일과 생존 표기법으로 플롯 생성

로짓 스케일과 생존 표기법을 사용하여 플롯을 생성합니다. 이는 set_yscale("logit", one_half="1/2", use_overline=True)를 사용하여 y 축 스케일을 로짓으로 설정하고, one_half 매개변수를 "1/2"로, use_overline 매개변수를 True로 설정함으로써 수행할 수 있습니다. 또한 plot()을 사용하여 정규, 라플라시안, 코시 분포의 누적 분포 함수를 플롯하고, legend()를 사용하여 범례를 추가합니다.

fig, axs = plt.subplots(nrows=1, ncols=1, figsize=(6.4, 4.8))

axs.plot(x, cdf_norm, label=r"$\mathcal{N}$")
axs.plot(x, cdf_laplacian, label=r"$\mathcal{L}$")
axs.plot(x, cdf_cauchy, label="Cauchy")
axs.set_yscale("logit", one_half="1/2", use_overline=True)
axs.set_ylim(1e-5, 1 - 1e-5)
axs.legend()
axs.grid()

plt.show()

선형 스케일 (linear scale) 로 플롯 생성

선형 스케일로 플롯을 생성합니다. 이는 plot()을 사용하여 정규, 라플라시안, 코시 분포의 누적 분포 함수를 플롯하고, legend()를 사용하여 범례를 추가함으로써 간단하게 수행할 수 있습니다.

fig, axs = plt.subplots(nrows=1, ncols=1, figsize=(6.4, 4.8))

axs.plot(x, cdf_norm, label=r"$\mathcal{N}$")
axs.plot(x, cdf_laplacian, label=r"$\mathcal{L}$")
axs.plot(x, cdf_cauchy, label="Cauchy")
axs.legend()
axs.grid()

plt.show()

요약

이 랩에서는 Matplotlib 에서 로짓 축 (logit axes) 을 사용하여 플롯을 생성하는 방법을 배웠습니다. 로짓 스케일과 표준 표기법, 로짓 스케일과 생존 표기법, 그리고 선형 스케일로 플롯을 생성했습니다. 이 랩에서는 math, numpy, 그리고 matplotlib.pyplot 라이브러리를 사용했습니다.