Matplotlib 이미지 그리드 컬러바

Beginner

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

소개

이 랩은 Matplotlib 을 사용하여 컬러바가 있는 이미지 그리드를 만드는 방법에 대한 것입니다. 제공된 예제 코드는 이미지 그리드의 각 행 또는 열에 대해 하나의 공통 컬러바를 사용하는 방법을 보여줍니다.

VM 팁

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

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

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

라이브러리 임포트

먼저, 컬러바가 있는 이미지 그리드를 생성하는 데 필요한 라이브러리를 임포트해야 합니다.

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

이미지 데이터 정의

샘플 이미지 데이터와 해당 범위를 반환하는 함수를 정의합니다.

def get_demo_image():
    z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")  ## 15x15 array
    return z, (-3, 4, -4, 3)

하단 컬러바가 있는 그리드 생성

각 열에 대한 컬러바가 있는 2x2 이미지 그리드를 생성합니다.

def demo_bottom_cbar(fig):
    grid = AxesGrid(fig, 121,  ## similar to subplot(121)
                    nrows_ncols=(2, 2),
                    axes_pad=0.10,
                    share_all=True,
                    label_mode="1",
                    cbar_location="bottom",
                    cbar_mode="edge",
                    cbar_pad=0.25,
                    cbar_size="15%",
                    direction="column"
                    )

    Z, extent = get_demo_image()
    cmaps = ["autumn", "summer"]
    for i in range(4):
        im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2])
        if i % 2:
            grid.cbar_axes[i//2].colorbar(im)

    for cax in grid.cbar_axes:
        cax.axis[cax.orientation].set_label("Bar")

    ## This affects all axes as share_all = True.
    grid.axes_llc.set_xticks([-2, 0, 2])
    grid.axes_llc.set_yticks([-2, 0, 2])

오른쪽 컬러바가 있는 그리드 생성

각 행에 대한 컬러바가 있는 2x2 이미지 그리드를 생성합니다.

def demo_right_cbar(fig):
    grid = AxesGrid(fig, 122,  ## similar to subplot(122)
                    nrows_ncols=(2, 2),
                    axes_pad=0.10,
                    label_mode="1",
                    share_all=True,
                    cbar_location="right",
                    cbar_mode="edge",
                    cbar_size="7%",
                    cbar_pad="2%",
                    )
    Z, extent = get_demo_image()
    cmaps = ["spring", "winter"]
    for i in range(4):
        im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2])
        if i % 2:
            grid.cbar_axes[i//2].colorbar(im)

    for cax in grid.cbar_axes:
        cax.axis[cax.orientation].set_label('Foo')

    ## This affects all axes because we set share_all = True.
    grid.axes_llc.set_xticks([-2, 0, 2])
    grid.axes_llc.set_yticks([-2, 0, 2])

Figure 생성 및 함수 호출

마지막으로, figure 를 생성하고 컬러바가 있는 이미지 그리드를 생성하기 위해 함수를 호출합니다.

fig = plt.figure()

demo_bottom_cbar(fig)
demo_right_cbar(fig)

plt.show()

요약

Matplotlib 은 AxesGrid 툴킷을 사용하여 컬러바가 있는 이미지 그리드를 생성하는 간단한 방법을 제공합니다. 이 랩에서는 각 열에 대한 컬러바가 있는 2x2 이미지 그리드와 각 행에 대한 컬러바가 있는 2x2 이미지 그리드를 생성하는 방법을 시연했습니다. 이러한 단계를 따르면, 여러분의 데이터셋에 대한 컬러바가 있는 이미지 그리드를 생성할 수 있습니다.