Matplotlib 로 이미지 그리드 생성

Beginner

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

소개

이 튜토리얼에서는 Matplotlib 의 ImageGrid를 사용하여 이미지 그리드를 만드는 방법을 보여줍니다. 2x2 이미지 그리드를 생성하고 그리드에 컬러바를 추가하는 다양한 방법을 살펴볼 것입니다.

VM 팁

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

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

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

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

먼저 그리드를 생성하기 위해 필요한 라이브러리와 데이터를 가져와야 합니다. 플로팅을 위해 matplotlib.pyplot을 사용하고, 샘플 데이터 세트를 얻기 위해 cbook을 사용하며, 그리드를 생성하기 위해 ImageGrid를 사용합니다.

import matplotlib.pyplot as plt
from matplotlib import cbook
from mpl_toolkits.axes_grid1 import ImageGrid

## Get sample data
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")  ## 15x15 array
extent = (-3, 4, -4, 3)

단일 컬러바를 가진 2x2 이미지 그리드 생성

첫 번째 그리드는 단일 컬러바를 가진 2x2 이미지 그리드가 될 것입니다. ImageGrid 함수를 사용하여 그리드를 생성하고 원하는 행과 열의 수를 지정합니다. 또한 컬러바의 위치를 지정하고 모든 이미지에서 컬러바를 공유하기 위해 share_allTrue로 설정합니다.

## Create a grid of 2x2 images with a single colorbar
grid = ImageGrid(
    fig,  ## Figure object
    141,  ## Location of subplot
    nrows_ncols=(2, 2),  ## Number of rows and columns
    axes_pad=0.0,  ## Padding between axes
    label_mode="L",  ## Label mode
    share_all=True,  ## Share colorbar across all images
    cbar_location="top",  ## Location of colorbar
    cbar_mode="single"  ## Colorbar mode
)

## Plot images on grid
for ax in grid:
    im = ax.imshow(Z, extent=extent)

## Add colorbar to grid
grid.cbar_axes[0].colorbar(im)
for cax in grid.cbar_axes:
    cax.tick_params(labeltop=False)

각 이미지에 자체 컬러바가 있는 2x2 이미지 그리드 생성

다음 그리드는 각 이미지에 자체 컬러바가 있는 2x2 이미지 그리드가 될 것입니다. ImageGrid 함수를 다시 사용하지만, 이번에는 cbar_mode"each"로 설정하여 각 이미지가 자체 컬러바를 가져야 함을 지정합니다.

## Create a grid of 2x2 images with each image having its own colorbar
grid = ImageGrid(
    fig,  ## Figure object
    142,  ## Location of subplot
    nrows_ncols=(2, 2),  ## Number of rows and columns
    axes_pad=0.1,  ## Padding between axes
    label_mode="1",  ## Label mode
    share_all=True,  ## Share colorbar across all images
    cbar_location="top",  ## Location of colorbar
    cbar_mode="each",  ## Colorbar mode
    cbar_size="7%",  ## Size of colorbar
    cbar_pad="2%"  ## Padding between colorbar and images
)

## Plot images on grid and add colorbars
for ax, cax in zip(grid, grid.cbar_axes):
    im = ax.imshow(Z, extent=extent)
    cax.colorbar(im)
    cax.tick_params(labeltop=False)

각 이미지에 자체 컬러바와 다른 컬러바 범위를 가진 2x2 이미지 그리드 생성

마지막 그리드는 또한 각 이미지에 자체 컬러바가 있는 2x2 이미지 그리드이지만, 이번에는 각 이미지에 대해 다른 컬러바 범위를 사용합니다. 각 이미지를 플로팅할 때 vminvmax를 사용하여 컬러바 범위를 설정합니다.

## Create a grid of 2x2 images with each image having its own colorbar and a different colorbar range
grid = ImageGrid(
    fig,  ## Figure object
    143,  ## Location of subplot
    nrows_ncols=(2, 2),  ## Number of rows and columns
    axes_pad=(0.45, 0.15),  ## Padding between axes
    label_mode="1",  ## Label mode
    share_all=True,  ## Share colorbar across all images
    cbar_location="right",  ## Location of colorbar
    cbar_mode="each",  ## Colorbar mode
    cbar_size="7%",  ## Size of colorbar
    cbar_pad="2%"  ## Padding between colorbar and images
)

## Plot images on grid and add colorbars
limits = ((0, 1), (-2, 2), (-1.7, 1.4), (-1.5, 1))  ## Different colorbar ranges
for ax, cax, vlim in zip(grid, grid.cbar_axes, limits):
    im = ax.imshow(Z, extent=extent, vmin=vlim[0], vmax=vlim[1])
    cb = cax.colorbar(im)
    cb.set_ticks((vlim[0], vlim[1]))

요약

이 튜토리얼에서는 Matplotlib 의 ImageGrid를 사용하여 이미지 그리드를 만드는 방법을 배웠습니다. 모든 이미지에 단일 컬러바를 사용하고, 각 이미지에 자체 컬러바를 제공하며, 각 이미지에 다른 컬러바 범위를 가진 자체 컬러바를 제공하는 등 그리드에 컬러바를 추가하는 다양한 방법을 살펴보았습니다.