Matplotlib 로그 축 플로팅

Beginner

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

소개

이 단계별 튜토리얼은 Python Matplotlib 을 사용하여 로그 축이 있는 플롯을 생성하는 과정을 안내합니다. 이 튜토리얼에서는 다음 주제를 다룹니다.

  1. Semilogy 플롯
  2. Semilogx 플롯
  3. Loglog 플롯
  4. Errorbars 플롯

VM 팁

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

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

학습 중에 문제가 발생하면 언제든지 Labby 에게 문의하십시오. 세션 후 피드백을 제공해주시면 문제를 즉시 해결해 드리겠습니다.

Semilogy 플롯

Semilogy 플롯은 y 축에 로그 스케일이 있는 플롯입니다. 값의 범위가 넓은 데이터를 시각화하는 데 유용합니다.

import matplotlib.pyplot as plt
import numpy as np

## Data for plotting
t = np.arange(0.01, 20.0, 0.01)

## Create figure
fig, ax1 = plt.subplots()

## Plot data on semilogy plot
ax1.semilogy(t, np.exp(-t / 5.0))

## Add title and grid to plot
ax1.set(title='Semilogy Plot')
ax1.grid()

## Display plot
plt.show()

Semilogx 플롯

Semilogx 플롯은 x 축에 로그 스케일이 있는 플롯입니다. x 축에 값의 범위가 넓은 데이터를 시각화하는 데 유용합니다.

import matplotlib.pyplot as plt
import numpy as np

## Data for plotting
t = np.arange(0.01, 20.0, 0.01)

## Create figure
fig, ax2 = plt.subplots()

## Plot data on semilogx plot
ax2.semilogx(t, np.sin(2 * np.pi * t))

## Add title and grid to plot
ax2.set(title='Semilogx Plot')
ax2.grid()

## Display plot
plt.show()

Loglog 플롯

Loglog 플롯은 x 축과 y 축 모두에 로그 스케일이 있는 플롯입니다. 두 축 모두에 값의 범위가 넓은 데이터를 시각화하는 데 유용합니다.

import matplotlib.pyplot as plt
import numpy as np

## Data for plotting
t = np.arange(0.01, 20.0, 0.01)

## Create figure
fig, ax3 = plt.subplots()

## Plot data on loglog plot
ax3.loglog(t, 20 * np.exp(-t / 10.0))

## Set x-axis scale to base 2
ax3.set_xscale('log', base=2)

## Add title and grid to plot
ax3.set(title='Loglog Plot')
ax3.grid()

## Display plot
plt.show()

Errorbars 플롯

Errorbars 플롯은 각 데이터 포인트에 대한 오차 막대를 표시하는 플롯입니다. 데이터 포인트가 음수 값을 가지는 경우 0.1 로 클리핑됩니다.

import matplotlib.pyplot as plt
import numpy as np

## Data for plotting
x = 10.0**np.linspace(0.0, 2.0, 20)
y = x**2.0

## Create figure
fig, ax4 = plt.subplots()

## Set x-axis and y-axis to logarithmic scale
ax4.set_xscale("log", nonpositive='clip')
ax4.set_yscale("log", nonpositive='clip')

## Plot data with error bars
ax4.errorbar(x, y, xerr=0.1 * x, yerr=5.0 + 0.75 * y)

## Set title and y-axis limit
ax4.set(title='Errorbars Plot')
ax4.set_ylim(bottom=0.1)

## Display plot
plt.show()

요약

Python Matplotlib 은 데이터 시각화를 생성하기 위한 강력한 도구입니다. 이 튜토리얼에서는 semilogy, semilogx, loglog 및 errorbars 플롯을 사용하여 로그 축이 있는 플롯을 만드는 방법을 다루었습니다. 이러한 유형의 플롯을 사용하면 값의 범위가 넓은 데이터를 효과적으로 시각화할 수 있습니다.