{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A set of error values, assume time step of 1 unit\n",
    "error_values = [\n",
    "  # variation at the start\n",
    "  10, 8, 3, -5, -10, -8, -3, \n",
    "  2, 3, 5, 4, 2, -3, -2,\n",
    "  # the steady state \n",
    "  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7faff88f2450>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 13224 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot the error values\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "plt.plot(np.arange(len(error_values)), [x for x in error_values], label=\"error\")\n",
    "# draw a middle line\n",
    "plt.plot(np.arange(len(error_values)), [0 for x in error_values], label=\"zero\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fafe9984450>]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 6128 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot error values as a series of bars\n",
    "plt.bar(np.arange(len(error_values)), [x for x in error_values], label=\"error\")\n",
    "# draw a middle line\n",
    "plt.plot(np.arange(len(error_values)), [0 for x in error_values], label=\"zero\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make another set, accumulate the error values\n",
    "error_values_accumulated = [0]\n",
    "for i in range(1, len(error_values)):\n",
    "  error_values_accumulated.append(error_values_accumulated[-1] + error_values[i])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 134252 characters]",
      "text/plain": [
       "<Figure size 3000x1500 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# set plot to 300 dpi\n",
    "plt.figure(figsize=(10, 5), dpi=300)\n",
    "# set the bar style to white fill with hatched lines in black\n",
    "plt.bar(np.arange(len(error_values)), [x for x in error_values], label=\"error\", hatch=\"//\", color=\"white\", edgecolor=\"black\")\n",
    "# draw a middle line\n",
    "plt.plot(np.arange(len(error_values)), [0 for x in error_values], label=\"zero\")\n",
    "# plot the accumulated error values as a line, extra thick\n",
    "plt.plot(np.arange(len(error_values_accumulated)), [x for x in error_values_accumulated], \n",
    "  label=\"accumulated error\", linewidth=2, color=\"black\")\n",
    "# reduce plot margins\n",
    "plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)\n",
    "\n",
    "# export plot as a png\n",
    "plt.savefig(\"error_plot.png\", bbox_inches=\"tight\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 432x288 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.4 ('.venv': venv)",
   "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.7.4"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "97190208b4a6552dc9493965a1caf8d39c9d1897fe9495f2fe88af90c5a07bd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
