{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Ishikawa Diagram\n\nIshikawa Diagrams, fishbone diagrams, herringbone diagrams, or cause-and-effect\ndiagrams are used to identify problems in a system by showing how causes and\neffects are linked.\nSource: https://en.wikipedia.org/wiki/Ishikawa_diagram\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import math\n\nimport matplotlib.pyplot as plt\n\nfrom matplotlib.patches import Polygon, Wedge\n\nfig, ax = plt.subplots(figsize=(10, 6), layout='constrained')\nax.set_xlim(-5, 5)\nax.set_ylim(-5, 5)\nax.axis('off')\n\n\ndef problems(data: str,\n             problem_x: float, problem_y: float,\n             angle_x: float, angle_y: float):\n    \"\"\"\n    Draw each problem section of the Ishikawa plot.\n\n    Parameters\n    ----------\n    data : str\n        The name of the problem category.\n    problem_x, problem_y : float, optional\n        The `X` and `Y` positions of the problem arrows (`Y` defaults to zero).\n    angle_x, angle_y : float, optional\n        The angle of the problem annotations. They are always angled towards\n        the tail of the plot.\n\n    Returns\n    -------\n    None.\n\n    \"\"\"\n    ax.annotate(str.upper(data), xy=(problem_x, problem_y),\n                xytext=(angle_x, angle_y),\n                fontsize=10,\n                color='white',\n                weight='bold',\n                xycoords='data',\n                verticalalignment='center',\n                horizontalalignment='center',\n                textcoords='offset fontsize',\n                arrowprops=dict(arrowstyle=\"->\", facecolor='black'),\n                bbox=dict(boxstyle='square',\n                          facecolor='tab:blue',\n                          pad=0.8))\n\n\ndef causes(data: list,\n           cause_x: float, cause_y: float,\n           cause_xytext=(-9, -0.3), top: bool = True):\n    \"\"\"\n    Place each cause to a position relative to the problems\n    annotations.\n\n    Parameters\n    ----------\n    data : indexable object\n        The input data. IndexError is\n        raised if more than six arguments are passed.\n    cause_x, cause_y : float\n        The `X` and `Y` position of the cause annotations.\n    cause_xytext : tuple, optional\n        Adjust to set the distance of the cause text from the problem\n        arrow in fontsize units.\n    top : bool, default: True\n        Determines whether the next cause annotation will be\n        plotted above or below the previous one.\n\n    Returns\n    -------\n    None.\n\n    \"\"\"\n    for index, cause in enumerate(data):\n        # [<x pos>, <y pos>]\n        coords = [[0.02, 0],\n                  [0.23, 0.5],\n                  [-0.46, -1],\n                  [0.69, 1.5],\n                  [-0.92, -2],\n                  [1.15, 2.5]]\n\n        # First 'cause' annotation is placed in the middle of the 'problems' arrow\n        # and each subsequent cause is plotted above or below it in succession.\n        cause_x -= coords[index][0]\n        cause_y += coords[index][1] if top else -coords[index][1]\n\n        ax.annotate(cause, xy=(cause_x, cause_y),\n                    horizontalalignment='center',\n                    xytext=cause_xytext,\n                    fontsize=9,\n                    xycoords='data',\n                    textcoords='offset fontsize',\n                    arrowprops=dict(arrowstyle=\"->\",\n                                    facecolor='black'))\n\n\ndef draw_body(data: dict):\n    \"\"\"\n    Place each problem section in its correct place by changing\n    the coordinates on each loop.\n\n    Parameters\n    ----------\n    data : dict\n        The input data (can be a dict of lists or tuples). ValueError\n        is raised if more than six arguments are passed.\n\n    Returns\n    -------\n    None.\n\n    \"\"\"\n    # Set the length of the spine according to the number of 'problem' categories.\n    length = (math.ceil(len(data) / 2)) - 1\n    draw_spine(-2 - length, 2 + length)\n\n    # Change the coordinates of the 'problem' annotations after each one is rendered.\n    offset = 0\n    prob_section = [1.55, 0.8]\n    for index, problem in enumerate(data.values()):\n        plot_above = index % 2 == 0\n        cause_arrow_y = 1.7 if plot_above else -1.7\n        y_prob_angle = 16 if plot_above else -16\n\n        # Plot each section in pairs along the main spine.\n        prob_arrow_x = prob_section[0] + length + offset\n        cause_arrow_x = prob_section[1] + length + offset\n        if not plot_above:\n            offset -= 2.5\n        if index > 5:\n            raise ValueError(f'Maximum number of problems is 6, you have entered '\n                             f'{len(data)}')\n\n        problems(list(data.keys())[index], prob_arrow_x, 0, -12, y_prob_angle)\n        causes(problem, cause_arrow_x, cause_arrow_y, top=plot_above)\n\n\ndef draw_spine(xmin: int, xmax: int):\n    \"\"\"\n    Draw main spine, head and tail.\n\n    Parameters\n    ----------\n    xmin : int\n        The default position of the head of the spine's\n        x-coordinate.\n    xmax : int\n        The default position of the tail of the spine's\n        x-coordinate.\n\n    Returns\n    -------\n    None.\n\n    \"\"\"\n    # draw main spine\n    ax.plot([xmin - 0.1, xmax], [0, 0], color='tab:blue', linewidth=2)\n    # draw fish head\n    ax.text(xmax + 0.1, - 0.05, 'PROBLEM', fontsize=10,\n            weight='bold', color='white')\n    semicircle = Wedge((xmax, 0), 1, 270, 90, fc='tab:blue')\n    ax.add_patch(semicircle)\n    # draw fish tail\n    tail_pos = [[xmin - 0.8, 0.8], [xmin - 0.8, -0.8], [xmin, -0.01]]\n    triangle = Polygon(tail_pos, fc='tab:blue')\n    ax.add_patch(triangle)\n\n\n# Input data\ncategories = {\n    'Method': ['Time consumption', 'Cost', 'Procedures', 'Inefficient process',\n               'Sampling'],\n    'Machine': ['Faulty equipment', 'Compatibility'],\n    'Material': ['Poor-quality input', 'Raw materials', 'Supplier',\n                 'Shortage'],\n    'Measurement': ['Calibration', 'Performance', 'Wrong measurements'],\n    'Environment': ['Bad conditions'],\n    'People': ['Lack of training', 'Managers', 'Labor shortage',\n               'Procedures', 'Sales strategy']\n}\n\ndraw_body(categories)\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}