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
import numpy as np

from matplotlib.colors import LightSource, Normalize

def display_colorbar():
    """Display a correct numeric colorbar for a shaded plot."""
    y, x = np.mgrid[-4:2:200j, -4:2:200j]
    z = 10 * np.cos(x**2 + y**2)

    cmap = plt.cm.copper
    ls = LightSource(315, 45)
    rgb = ls.shade(z, cmap)

    fig, ax = plt.subplots()
    ax.imshow(rgb, interpolation='bilinear')

    ## Use a proxy artist for the colorbar...
    im = ax.imshow(z, cmap=cmap)
    im.remove()
    fig.colorbar(im, ax=ax)

    ax.set_title('Using a colorbar with a shaded plot', size='x-large')

음영 처리된 플롯에서 이상치 제거하기

이 단계에서는 사용자 정의 norm 을 사용하여 음영 처리된 플롯의 표시되는 z-범위를 제어하는 방법을 배우게 됩니다.

def avoid_outliers():
    """Use a custom norm to control the displayed z-range of a shaded plot."""
    y, x = np.mgrid[-4:2:200j, -4:2:200j]
    z = 10 * np.cos(x**2 + y**2)

    ## Add some outliers...
    z[100, 105] = 2000
    z[120, 110] = -9000

    ls = LightSource(315, 45)
    fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4.5))

    rgb = ls.shade(z, plt.cm.copper)
    ax1.imshow(rgb, interpolation='bilinear')
    ax1.set_title('Full range of data')

    rgb = ls.shade(z, plt.cm.copper, vmin=-10, vmax=10)
    ax2.imshow(rgb, interpolation='bilinear')
    ax2.set_title('Manually set range')

    fig.suptitle('Avoiding Outliers in Shaded Plots', size='x-large')

음영 및 색상을 통해 다른 변수 표시하기

이 단계에서는 음영 및 색상을 통해 다른 변수를 표시하는 방법을 배우게 됩니다.

def shade_other_data():
    """Demonstrates displaying different variables through shade and color."""
    y, x = np.mgrid[-4:2:200j, -4:2:200j]
    z1 = np.sin(x**2)  ## Data to hillshade
    z2 = np.cos(x**2 + y**2)  ## Data to color

    norm = Normalize(z2.min(), z2.max())
    cmap = plt.cm.RdBu

    ls = LightSource(315, 45)
    rgb = ls.shade_rgb(cmap(norm(z2)), z1)

    fig, ax = plt.subplots()
    ax.imshow(rgb, interpolation='bilinear')
    ax.set_title('Shade by one variable, color by another', size='x-large')

요약

이 랩에서는 다양한 기술을 사용하여 Matplotlib 에서 음영 처리된 플롯을 만드는 방법을 배웠습니다. 여기에는 음영 처리된 플롯에 대한 colorbar 표시, 음영 처리된 플롯에서 이상치 제거, 그리고 음영 및 색상을 통해 다른 변수를 표시하는 방법이 포함됩니다. 이러한 기술은 다양한 응용 분야에서 데이터를 시각화하고 탐색하는 데 유용할 수 있습니다.