石川馨図の作成

PythonPythonBeginner
今すぐ練習

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

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、魚骨図または因果図としても知られる石川図を作成する方法を学びます。石川図は、原因と結果がどのように関連しているかを示すことで、システム内の問題を特定するために一般的に使用されます。この図を作成するために、Python と Matplotlib ライブラリを使用します。

VM のヒント

VM の起動が完了したら、左上隅をクリックして ノートブック タブに切り替え、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')

関数を定義する

この図を作成するために使用する 3 つの関数を定義します。

問題関数

最初の関数は、問題関数です。この関数は、カテゴリ名と、問題矢印の x と y 座標、および問題の注釈の角度を受け取ります。注釈メソッドを使用して、問題矢印と注釈を作成します。

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))
原因関数

2 番目の関数は、原因関数です。この関数は、原因のリストと、原因の注釈の x と y 座標、および原因が問題矢印の上または下に配置されるかどうかを受け取ります。注釈メソッドを使用して、原因の注釈と矢印を作成します。

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'))
本体を描画する関数

3 番目の関数は、本体を描画する関数です。この関数は、入力データを受け取り、それを使って魚骨図を作成します。

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 ライブラリを使って石川馨図を作成する方法を学びました。図を作成するために 3 つの関数を定義し、辞書を使って入力データを定義しました。結果として得られる図は、原因と結果がシステム内でどのように関連付けられているかを示しており、問題の特定に役立ちます。