Sankey 다이어그램 생성

Beginner

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

소개

Sankey 다이어그램은 서로 다른 단계 또는 시스템 간의 자원 또는 에너지 이동을 보여주는 흐름 다이어그램입니다. 이 튜토리얼에서는 Python 의 Matplotlib 라이브러리를 사용하여 Sankey 다이어그램을 만들 것입니다.

VM 팁

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

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

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

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

Sankey 다이어그램을 만들기 전에 필요한 라이브러리를 가져와야 합니다. 이 튜토리얼에서는 Matplotlib 라이브러리를 사용할 것입니다.

import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey

간단한 Sankey 다이어그램 만들기

Sankey 클래스를 사용하는 방법을 보여주는 간단한 Sankey 다이어그램을 만드는 것으로 시작합니다.

Sankey(flows=[0.25, 0.15, 0.60, -0.20, -0.15, -0.05, -0.50, -0.10],
       labels=['', '', '', 'First', 'Second', 'Third', 'Fourth', 'Fifth'],
       orientations=[-1, 1, 0, 1, 1, 1, 0, -1]).finish()
plt.title("The default settings produce a diagram like this.")

이 코드는 기본 설정을 사용하여 Sankey 다이어그램을 생성합니다. 여기에는 흐름의 레이블과 방향이 포함됩니다. 결과 다이어그램은 "The default settings produce a diagram like this."라는 제목으로 표시됩니다.

Sankey 다이어그램 사용자 정의

흐름 (flows), 레이블 (labels), 방향 (orientations) 및 기타 매개변수를 변경하여 Sankey 다이어그램을 사용자 정의할 수 있습니다. 이 예제에서는 더 긴 경로와 중간에 레이블이 있는 다이어그램을 만들 것입니다.

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
                     title="Flow Diagram of a Widget")
sankey = Sankey(ax=ax, scale=0.01, offset=0.2, head_angle=180,
                format='%.0f', unit='%')
sankey.add(flows=[25, 0, 60, -10, -20, -5, -15, -10, -40],
           labels=['', '', '', 'First', 'Second', 'Third', 'Fourth',
                   'Fifth', 'Hurray!'],
           orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0],
           pathlengths=[0.25, 0.25, 0.25, 0.25, 0.25, 0.6, 0.25, 0.25,
                        0.25],
           patchlabel="Widget\nA")  ## Arguments to matplotlib.patches.PathPatch
diagrams = sankey.finish()
diagrams[0].texts[-1].set_color('r')
diagrams[0].text.set_fontweight('bold')

이 코드는 더 긴 경로, 중간에 레이블, 그리고 기타 사용자 정의된 매개변수를 가진 Sankey 다이어그램을 생성합니다. 결과 다이어그램은 "Flow Diagram of a Widget"라는 제목으로 표시됩니다.

Sankey 다이어그램에서 두 시스템 연결하기

Sankey 다이어그램에서 두 시스템을 연결할 수도 있습니다. 이 예제에서는 연결된 두 시스템이 있는 다이어그램을 만들 것입니다.

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Two Systems")
flows = [0.25, 0.15, 0.60, -0.10, -0.05, -0.25, -0.15, -0.10, -0.35]
sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=flows, label='one',
           orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0])
sankey.add(flows=[-0.25, 0.15, 0.1], label='two',
           orientations=[-1, -1, -1], prior=0, connect=(0, 0))
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend()

이 코드는 연결된 두 시스템이 있는 Sankey 다이어그램을 생성합니다. 결과 다이어그램은 "Two Systems"라는 제목으로 표시됩니다.

요약

이 튜토리얼에서는 Python 의 Matplotlib 라이브러리를 사용하여 Sankey 다이어그램을 만드는 방법을 배웠습니다. 간단한 다이어그램으로 시작하여 흐름 (flows), 레이블 (labels), 방향 (orientations) 및 기타 매개변수를 변경하여 사용자 정의했습니다. 또한 Sankey 다이어그램에서 두 시스템을 연결하는 방법도 배웠습니다. 이러한 도구를 사용하여 다양한 응용 분야에 유용하고 시각적으로 매력적인 흐름 다이어그램을 만들 수 있습니다.