石川图(也称为鱼骨图、人字形图、因果图)是由石川薰创建的因果图,用于显示特定事件的潜在原因。基本概念于 1920 年代首次使用,被认为是质量控制的七种基本工具之一。它因其形状而被称为鱼骨图,类似于鱼骨架的侧视图。
import math
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon, Wedge
fig, ax = plt.subplots(figsize=(10, 6), layout='constrained')
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
ax.axis('off')
def problems(data: str,
problem_x: float, problem_y: float,
angle_x: float, angle_y: float):
"""
Draw each problem section of the Ishikawa plot.
Parameters
----------
data : str
问题类别的名称。
problem_x, problem_y : float, optional
问题箭头的`X`和`Y`位置(`Y`默认为零)。
angle_x, angle_y : float, optional
问题注释的角度。它们总是朝向图表尾部倾斜。
Returns
-------
None.
"""
'使用 annotate 方法添加文本和箭头。'
ax.annotate(str.upper(data), xy=(problem_x, problem_y),
xytext=(angle_x, 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))
def causes(data: list,
cause_x: float, cause_y: float,
cause_xytext=(-9, -0.3), top: bool = True):
"""
Place each cause to a position relative to the problems
annotations.
Parameters
----------
data : indexable object
可索引对象,输入数据。如果传递了超过六个参数,会引发 IndexError。
cause_x, cause_y : float
表示原因注释的 X 和 Y 位置
cause_xytext : tuple, optional
用于调整原因文本与问题箭头之间的距离,以字号单位表示。
top : bool, default: True
决定下一个原因注释是绘制在前一个注释的上方还是下方。
Returns
-------
None.
"""
'使用 enumerate 遍历 data 列表中的每个原因'
for index, cause in enumerate(data):
# [<x pos>, <y pos>]
coords = [[0.02, 0],
[0.23, 0.5],
[-0.46, -1],
[0.69, 1.5],
[-0.92, -2],
[1.15, 2.5]]
# First 'cause' annotation is placed in the middle of the 'problems' arrow
# and each subsequent cause is plotted above or below it in succession.
#首先将“原因”注释放置在“问题”箭头的中间,然后依次在其上方或下方绘制每个后续的原因。
cause_x -= coords[index][0]
cause_y += coords[index][1] if top else -coords[index][1]
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):
"""
Place each problem section in its correct place by changing
the coordinates on each loop.
Parameters
----------
data : dict
输入数据(可以是列表或元组的字典)。如果传递了超过六个参数,会引发 ValueError。
Returns
-------
None.
"""
# Set the length of the spine according to the number of 'problem' categories.
#根据“问题”类别的数量设置脊柱的长度
length = (math.ceil(len(data) / 2)) - 1
draw_spine(-2 - length, 2 + length)
# Change the coordinates of the 'problem' annotations after each one is rendered.
#在每个问题注释渲染后,改变其坐标。
offset = 0
prob_section = [1.55, 0.8]
for index, problem in enumerate(data.values()):
plot_above = index % 2 == 0
cause_arrow_y = 1.7 if plot_above else -1.7
y_prob_angle = 16 if plot_above else -16
# Plot each section in pairs along the main spine.
#沿着主脊柱成对地绘制每个部分。
prob_arrow_x = prob_section[0] + length + offset
cause_arrow_x = prob_section[1] + length + offset
if not plot_above:
offset -= 2.5
if index > 5:
raise ValueError(f'Maximum number of problems is 6, you have entered '
f'{len(data)}')
problems(list(data.keys())[index], prob_arrow_x, 0, -12, y_prob_angle)
causes(problem, cause_arrow_x, cause_arrow_y, top=plot_above)
def draw_spine(xmin: int, xmax: int):
"""
Draw main spine, head and tail.
Parameters
----------
xmin : int
脊柱头部的默认 x 坐标位置。
xmax : int
脊柱尾部的默认 x 坐标位置。
Returns
-------
None.
"""
# draw main spine
ax.plot([xmin - 0.1, xmax], [0, 0], color='tab:blue', linewidth=2)
# draw fish head
ax.text(xmax + 0.1, - 0.05, 'PROBLEM', fontsize=10,
weight='bold', color='white')
semicircle = Wedge((xmax, 0), 1, 270, 90, fc='tab:blue')
ax.add_patch(semicircle)
# draw fish tail
tail_pos = [[xmin - 0.8, 0.8], [xmin - 0.8, -0.8], [xmin, -0.01]]
triangle = Polygon(tail_pos, fc='tab:blue')
ax.add_patch(triangle)
# Input data
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()
石川图的常见用途是产品设计质量缺陷预防,以确定导致整体效果的潜在因素。不完美的每个原因或原因都是变化的来源。原因通常分为主要类别,以识别和分类这些变异来源。马自达汽车在开发 Miata (MX5) 跑车时使用了石川图。