Matplotlib Pcolor 시각화 튜토리얼

Beginner

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

소개

이 튜토리얼은 Matplotlib 에서 Pcolor 를 사용하는 방법에 대한 소개입니다. Pcolor 를 사용하면 2D 이미지 스타일의 플롯을 생성할 수 있으며, Matplotlib 에서 이를 사용하는 방법을 보여드리겠습니다.

VM 팁

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

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

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

간단한 Pcolor 데모

첫 번째 단계는 간단한 Pcolor 데모를 만드는 것입니다. 이것은 기본적인 Pcolor 플롯을 생성하는 방법을 보여줍니다.

Z = np.random.rand(6, 10)

fig, (ax0, ax1) = plt.subplots(2, 1)

c = ax0.pcolor(Z)
ax0.set_title('default: no edges')

c = ax1.pcolor(Z, edgecolors='k', linewidths=4)
ax1.set_title('thick edges')

fig.tight_layout()
plt.show()

Pcolor 와 유사한 함수 비교

두 번째 단계는 Pcolor 를 Pcolormesh, Imshow 및 Pcolorfast 와 같은 유사한 함수와 비교하는 것입니다. 이를 통해 이러한 함수 간의 차이점과 각 함수를 언제 사용해야 하는지 이해하는 데 도움이 됩니다.

## make these smaller to increase the resolution
dx, dy = 0.15, 0.05

## generate 2 2d grids for the x & y bounds
y, x = np.mgrid[-3:3+dy:dy, -3:3+dx:dx]
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
## x and y are bounds, so z should be the value *inside* those bounds.
## Therefore, remove the last value from the z array.
z = z[:-1, :-1]
z_min, z_max = -abs(z).max(), abs(z).max()

fig, axs = plt.subplots(2, 2)

ax = axs[0, 0]
c = ax.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
ax.set_title('pcolor')
fig.colorbar(c, ax=ax)

ax = axs[0, 1]
c = ax.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
ax.set_title('pcolormesh')
fig.colorbar(c, ax=ax)

ax = axs[1, 0]
c = ax.imshow(z, cmap='RdBu', vmin=z_min, vmax=z_max,
              extent=[x.min(), x.max(), y.min(), y.max()],
              interpolation='nearest', origin='lower', aspect='auto')
ax.set_title('image (nearest, aspect="auto")')
fig.colorbar(c, ax=ax)

ax = axs[1, 1]
c = ax.pcolorfast(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
ax.set_title('pcolorfast')
fig.colorbar(c, ax=ax)

fig.tight_layout()
plt.show()

로그 스케일의 Pcolor

세 번째 단계는 로그 스케일로 Pcolor 플롯을 만드는 것입니다. 이는 광범위한 값 범위를 가진 데이터를 사용할 때 유용합니다.

N = 100
X, Y = np.meshgrid(np.linspace(-3, 3, N), np.linspace(-2, 2, N))

## A low hump with a spike coming out.
## Needs to have z/colour axis on a log scale, so we see both hump and spike.
## A linear scale only shows the spike.
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
Z = Z1 + 50 * Z2

fig, (ax0, ax1) = plt.subplots(2, 1)

c = ax0.pcolor(X, Y, Z, shading='auto',
               norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap='PuBu_r')
fig.colorbar(c, ax=ax0)

c = ax1.pcolor(X, Y, Z, cmap='PuBu_r', shading='auto')
fig.colorbar(c, ax=ax1)

plt.show()

요약

이 튜토리얼에서는 Matplotlib 에서 Pcolor 를 사용하는 방법을 보여드렸습니다. 간단한 Pcolor 데모로 시작하여 Pcolor 를 Pcolormesh 및 Imshow 와 같은 유사한 함수와 비교했습니다. 마지막으로 로그 스케일로 Pcolor 플롯을 만들었습니다.