简介
在本实验中,我们将学习如何创建石川图(也称为鱼骨图或因果图)。石川图通常用于通过展示原因和结果之间的联系来识别系统中的问题。我们将使用 Python 和 Matplotlib 库来创建该图。
虚拟机使用提示
虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问 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
创建鱼骨图
现在我们将创建鱼骨图。我们将首先创建一个图形和轴对象。
fig, ax = plt.subplots(figsize=(10, 6), layout='constrained')
接下来,我们将设置轴的 x 和 y 限制,并关闭轴。
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
ax.axis('off')
定义函数
我们将定义三个用于创建图表的函数。
问题函数
第一个函数是问题函数。该函数接受类别名称、问题箭头的 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))
原因函数
第二个函数是原因函数。该函数接受原因列表、原因注释的 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'))
绘制主体函数
第三个函数是绘制主体函数。该函数接受输入数据并使用它来创建鱼骨图。
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(categories)
plt.show()
总结
在本实验中,我们学习了如何使用 Python 和 Matplotlib 库创建石川图。我们定义了三个函数来创建图表,并使用字典定义输入数据。生成的图表展示了系统中原因和结果是如何关联的,可用于识别问题。