소개
이 랩에서는 물고기 뼈 다이어그램 또는 인과 관계 다이어그램이라고도 하는 이시카와 다이어그램을 만드는 방법을 배웁니다. 이시카와 다이어그램은 원인과 결과가 어떻게 연결되어 있는지 보여줌으로써 시스템의 문제를 식별하는 데 일반적으로 사용됩니다. Python 과 Matplotlib 라이브러리를 사용하여 다이어그램을 만들 것입니다.
VM 팁
VM 시작이 완료되면 왼쪽 상단을 클릭하여 Notebook 탭으로 전환하여 실습을 위해 Jupyter Notebook에 액세스하십시오.
때로는 Jupyter Notebook 이 로딩을 완료하는 데 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한 사항으로 인해 작업의 유효성 검사는 자동화할 수 없습니다.
학습 중에 문제가 발생하면 언제든지 Labby 에게 문의하십시오. 세션 후 피드백을 제공해주시면 문제를 즉시 해결해 드리겠습니다.
Matplotlib 설치
시작하기 전에 Matplotlib 이 설치되어 있는지 확인해야 합니다. 아직 설치하지 않았다면 다음 명령을 사용하여 설치할 수 있습니다.
!pip install matplotlib
라이브러리 가져오기
필요한 라이브러리를 가져오는 것으로 시작합니다. Matplotlib 와 matplotlib.patches 모듈의 Polygon 및 Wedge 클래스를 사용합니다.
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon, Wedge
피쉬본 다이어그램 (Fishbone Diagram) 생성
이제 피쉬본 다이어그램을 생성합니다. 먼저 figure 와 axis 객체를 생성합니다.
fig, ax = plt.subplots(figsize=(10, 6), layout='constrained')
다음으로, axis 의 x 및 y 제한을 설정하고 axis 를 끕니다.
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
ax.axis('off')
함수 정의
다이어그램을 생성하는 데 사용할 세 가지 함수를 정의합니다.
Problems 함수
첫 번째 함수는 problems 함수입니다. 이 함수는 카테고리 이름, 문제 화살표의 x 및 y 위치, 문제 주석의 각도를 입력으로 받습니다. annotate 메서드를 사용하여 문제 화살표와 주석을 생성합니다.
def problems(data: str,
problem_x: float, problem_y: float,
prob_angle_x: float, prob_angle_y: float):
ax.annotate(str.upper(data), xy=(problem_x, problem_y),
xytext=(prob_angle_x, prob_angle_y),
fontsize='10',
color='white',
weight='bold',
xycoords='data',
verticalalignment='center',
horizontalalignment='center',
textcoords='offset fontsize',
arrowprops=dict(arrowstyle="->", facecolor='black'),
bbox=dict(boxstyle='square',
facecolor='tab:blue',
pad=0.8))
Causes 함수
두 번째 함수는 causes 함수입니다. 이 함수는 원인 목록, 원인 주석의 x 및 y 위치, 그리고 원인을 문제 화살표 위 또는 아래에 배치할지 여부를 입력으로 받습니다. annotate 메서드를 사용하여 원인 주석과 화살표를 생성합니다.
def causes(data: list, cause_x: float, cause_y: float,
cause_xytext=(-9, -0.3), top: bool = True):
for index, cause in enumerate(data):
coords = [[0, [0, 0]],
[0.23, [0.5, -0.5]],
[-0.46, [-1, 1]],
[0.69, [1.5, -1.5]],
[-0.92, [-2, 2]],
[1.15, [2.5, -2.5]]]
if top:
cause_y += coords[index][1][0]
else:
cause_y += coords[index][1][1]
cause_x -= coords[index][0]
ax.annotate(cause, xy=(cause_x, cause_y),
horizontalalignment='center',
xytext=cause_xytext,
fontsize='9',
xycoords='data',
textcoords='offset fontsize',
arrowprops=dict(arrowstyle="->",
facecolor='black'))
Draw Body 함수
세 번째 함수는 draw body 함수입니다. 이 함수는 입력 데이터를 받아 피쉬본 다이어그램을 생성하는 데 사용합니다.
def draw_body(data: dict):
second_sections = []
third_sections = []
if len(data) == 1 or len(data) == 2:
spine_length = (-2.1, 2)
head_pos = (2, 0)
tail_pos = ((-2.8, 0.8), (-2.8, -0.8), (-2.0, -0.01))
first_section = [1.6, 0.8]
elif len(data) == 3 or len(data) == 4:
spine_length = (-3.1, 3)
head_pos = (3, 0)
tail_pos = ((-3.8, 0.8), (-3.8, -0.8), (-3.0, -0.01))
first_section = [2.6, 1.8]
second_sections = [-0.4, -1.2]
else: ## len(data) == 5 or 6
spine_length = (-4.1, 4)
head_pos = (4, 0)
tail_pos = ((-4.8, 0.8), (-4.8, -0.8), (-4.0, -0.01))
first_section = [3.5, 2.7]
second_sections = [1, 0.2]
third_sections = [-1.5, -2.3]
for index, problem in enumerate(data.values()):
top_row = True
cause_arrow_y = 1.7
if index % 2 != 0:
top_row = False
y_prob_angle = -16
cause_arrow_y = -1.7
else:
y_prob_angle = 16
if index in (0, 1):
prob_arrow_x = first_section[0]
cause_arrow_x = first_section[1]
elif index in (2, 3):
prob_arrow_x = second_sections[0]
cause_arrow_x = second_sections[1]
else:
prob_arrow_x = third_sections[0]
cause_arrow_x = third_sections[1]
if index > 5:
raise ValueError(f'Maximum number of problems is 6, you have entered '
f'{len(data)}')
ax.plot(spine_length, [0, 0], color='tab:blue', linewidth=2)
ax.text(head_pos[0] + 0.1, head_pos[1] - 0.05, 'PROBLEM', fontsize=10,
weight='bold', color='white')
semicircle = Wedge(head_pos, 1, 270, 90, fc='tab:blue')
ax.add_patch(semicircle)
triangle = Polygon(tail_pos, fc='tab:blue')
ax.add_patch(triangle)
problems(list(data.keys())[index], prob_arrow_x, 0, -12, y_prob_angle)
causes(problem, cause_arrow_x, cause_arrow_y, top=top_row)
입력 데이터
이제 입력 데이터를 정의합니다. 데이터는 키가 카테고리이고 값은 원인 목록인 딕셔너리여야 합니다.
categories = {
'Method': ['Time consumption', 'Cost', 'Procedures', 'Inefficient process', 'Sampling'],
'Machine': ['Faulty equipment', 'Compatibility'],
'Material': ['Poor-quality input', 'Raw materials', 'Supplier', 'Shortage'],
'Measurement': ['Calibration', 'Performance', 'Wrong measurements'],
'Environment': ['Bad conditions'],
'People': ['Lack of training', 'Managers', 'Labor shortage', 'Procedures', 'Sales strategy']
}
피쉬본 다이어그램 그리기
마지막으로, draw body 함수를 호출하고 다이어그램을 표시합니다.
draw_body(categories)
plt.show()
요약
이 랩에서는 Python 과 Matplotlib 라이브러리를 사용하여 Ishikawa 다이어그램을 만드는 방법을 배웠습니다. 다이어그램을 만들기 위해 세 가지 함수를 정의하고 입력 데이터를 정의하기 위해 딕셔너리를 사용했습니다. 결과 다이어그램은 원인과 결과가 시스템에서 어떻게 연결되어 있는지 보여주며 문제를 식별하는 데 사용될 수 있습니다.