{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fc1f0018d90>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14852 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# create a figure containing a single axes\n",
    "fig, ax = plt.subplots()\n",
    "# plot some data on the axes\n",
    "ax.plot([1, 2, 3, 4], [1, 4, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fc1eff84dc0>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14852 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot on \"current\" axes\n",
    "plt.plot([1, 2, 3, 4], [1, 4, 2, 3])"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 128520 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 432x288 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 4740 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 8216 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure()              # an empty figure with no Axes\n",
    "fig, ax = plt.subplots()        # an figure with a single Axes\n",
    "fig, axs = plt.subplots(2, 2)   # a figure with a 2x2 grid of Axes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "all plot functions expect `numpy.array` as input, 'array-like' such as `pandas` data object\n",
    "and `numpy.matrix` may or may not work as intended"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7fc1eea9dbb0>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 22168 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Using Matplotlib in Object-Oriented style: suitable for Embedded GUI plotting\n",
    "# Example: https://matplotlib.org/3.3.0/gallery/user_interfaces/embedding_in_gtk3_panzoom_sgskip.html\n",
    "\n",
    "x = np.linspace(0, 2, 100)\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.plot(x, x, label='linear')\n",
    "ax.plot(x, x * 2, label='quadratic')\n",
    "ax.plot(x, x ** 3, label=\"cubic\")\n",
    "ax.set_xlabel('x label')\n",
    "ax.set_ylabel('y label')\n",
    "ax.set_title('Sample Plot')\n",
    "ax.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7fc1eebb0eb0>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 21744 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Using Matplotlib in pylot sytle: suitable for Interactive plotting like Jupyter Notebook\n",
    "\n",
    "x = np.linspace(0, 2, 100)\n",
    "\n",
    "plt.plot(x, x, label='linear')\n",
    "plt.plot(x, x ** 2, label='quadratic')\n",
    "plt.plot(x, x ** 3, label='cubic')\n",
    "plt.xlabel('x label')\n",
    "plt.ylabel('y label')\n",
    "plt.title('Sample Plot')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fc1eeed9c40>]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 64612 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 2 sub-plots:\n",
    "data1, data2, data3, data4 = np.random.randn(4, 100)\n",
    "\n",
    "fig, (ax1, ax2) = plt.subplots(1, 2)  # 1 row 2 columns\n",
    "ax1.plot(data1, data2, marker='x')\n",
    "ax2.plot(data3, data4, marker='o')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fc1ee78c070>]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 8872 characters]\n",
      "text/plain": [
       "<Figure size 864x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# formatting style\n",
    "\n",
    "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\n",
    "ax1.plot([1, 2, 3, 4], [1, 4, 9, 16], 'b-')    # default blue-line style\n",
    "ax2.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')    # red-cricle style"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fc1ee7057f0>,\n",
       " <matplotlib.lines.Line2D at 0x7fc1ee7056a0>,\n",
       " <matplotlib.lines.Line2D at 0x7fc1ee705820>]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 8176 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plotting multiple lines on one Axes\n",
    "\n",
    "x = np.arange(0., 5., .2)\n",
    "plt.plot(\n",
    "    x, x, 'r--',\n",
    "    x, x ** 2, 'bs',\n",
    "    x, x ** 3, 'g^',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x7fc1eee57040>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 26916 characters]\n",
      "text/plain": [
       "<Figure size 864x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plotting with keyword string: suitable for table-like data such as pandas.DataFrame\n",
    "\n",
    "data = {\n",
    "    'x': np.arange(50),\n",
    "    'y': np.arange(50) + np.random.randn(50) * 10,\n",
    "    'colors': np.random.randint(0, 50, 50),\n",
    "    'sizes': np.abs(np.random.randn(50)) * 100,\n",
    "}\n",
    "\n",
    "plt.subplots(figsize=(12, 10))\n",
    "plt.scatter('x', 'y', c='colors', s='sizes', data=data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0.98, 'Categorical Plotting')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14476 characters]\n",
      "text/plain": [
       "<Figure size 864x288 with 3 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# pyplot-style subplots\n",
    "\n",
    "names = ['foo', 'bar', 'foobar']\n",
    "values = [1, 10, 100]\n",
    "\n",
    "plt.figure(figsize=(12, 4))\n",
    "\n",
    "# 131\n",
    "# first digit 1 is the number of rows, \n",
    "# second digit is number of columns, \n",
    "# and last one is the index\n",
    "plt.subplot(131)\n",
    "plt.bar(names, values)\n",
    "plt.title('bar')\n",
    "\n",
    "plt.subplot(132)\n",
    "plt.scatter(names, values)\n",
    "plt.title('scatter')\n",
    "\n",
    "plt.subplot(133)\n",
    "plt.plot(names, values)\n",
    "plt.title('line')\n",
    "\n",
    "plt.suptitle('Categorical Plotting')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array\n",
      "  alpha: float or None\n",
      "  animated: bool\n",
      "  antialiased or aa: bool\n",
      "  clip_box: `.Bbox`\n",
      "  clip_on: bool\n",
      "  clip_path: Patch or (Path, Transform) or None\n",
      "  color or c: color\n",
      "  contains: unknown\n",
      "  dash_capstyle: {'butt', 'round', 'projecting'}\n",
      "  dash_joinstyle: {'miter', 'round', 'bevel'}\n",
      "  dashes: sequence of floats (on/off ink in points) or (None, None)\n",
      "  data: (2, N) array or two 1D arrays\n",
      "  drawstyle or ds: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps-post'}, default: 'default'\n",
      "  figure: `.Figure`\n",
      "  fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none'}\n",
      "  gid: str\n",
      "  in_layout: bool\n",
      "  label: object\n",
      "  linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}\n",
      "  linewidth or lw: float\n",
      "  marker: marker style string, `~.path.Path` or `~.markers.MarkerStyle`\n",
      "  markeredgecolor or mec: color\n",
      "  markeredgewidth or mew: float\n",
      "  markerfacecolor or mfc: color\n",
      "  markerfacecoloralt or mfcalt: color\n",
      "  markersize or ms: float\n",
      "  markevery: None or int or (int, int) or slice or List[int] or float or (float, float) or List[bool]\n",
      "  path_effects: `.AbstractPathEffect`\n",
      "  picker: unknown\n",
      "  pickradius: float\n",
      "  rasterized: bool or None\n",
      "  sketch_params: (scale: float, length: float, randomness: float)\n",
      "  snap: bool or None\n",
      "  solid_capstyle: {'butt', 'round', 'projecting'}\n",
      "  solid_joinstyle: {'miter', 'round', 'bevel'}\n",
      "  transform: `matplotlib.transforms.Transform`\n",
      "  url: str\n",
      "  visible: bool\n",
      "  xdata: 1D array\n",
      "  ydata: 1D array\n",
      "  zorder: float\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 7376 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 6344 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 13992 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# controlling line properties\n",
    "\n",
    "x = np.linspace(0, 2, 100)\n",
    "\n",
    "line, = plt.plot(x, x ** 2, linewidth=3.0)\n",
    "line.set_antialiased(False)\n",
    "\n",
    "plt.figure()\n",
    "lines = plt.plot(x, x ** 3, '--')\n",
    "plt.setp(lines, color='r', linewidth=2.0, antialiased=False)\n",
    "\n",
    "plt.figure()\n",
    "lines = plt.plot([1, 2, 3])\n",
    "plt.setp(lines)  # get a list of settable line properties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Y')"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 12936 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# working with text\n",
    "x = np.linspace(0, 4, 100)\n",
    "plt.plot(x, np.exp(x))\n",
    "plt.text(2.0, 20, r'$y=e^x$')\n",
    "plt.grid(True)\n",
    "plt.xlabel('X', fontsize=14, color='red')\n",
    "plt.ylabel('Y', fontsize=14, color='blue')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(3, 1.5, 'local max')"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 34932 characters]\n",
      "text/plain": [
       "<Figure size 864x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# annotating text\n",
    "\n",
    "plt.figure(figsize=(12, 4))\n",
    "t = np.arange(0.0, 5.0, 0.01)\n",
    "s = np.cos(2 * np.pi * t)\n",
    "line, = plt.plot(t, s, lw=2)\n",
    "\n",
    "plt.annotate('local max',\n",
    "             xy=(2, 1),          # target point\n",
    "             xytext=(3, 1.5),    # text location\n",
    "             arrowprops={'facecolor': 'black', 'shrink': 0.05}\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 61992 characters]\n",
      "text/plain": [
       "<Figure size 864x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 93836 characters]\n",
      "text/plain": [
       "<Figure size 864x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 49580 characters]\n",
      "text/plain": [
       "<Figure size 864x864 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# logarithmic and other nonlinear axes\n",
    "\n",
    "# fixing random state for reproducibility\n",
    "np.random.seed(19680801)\n",
    "\n",
    "# make up some datain the open interval (0, 1)\n",
    "y = np.random.normal(loc=0.5, scale=0.4, size=1000)\n",
    "plt.figure(figsize=(12, 4))\n",
    "plt.plot(y)\n",
    "plt.title('guassian distribution, loc=0.5, scale=0.4, size=1000')\n",
    "y = y[(y > 0) & (y < 1)]\n",
    "plt.figure(figsize=(12, 4))\n",
    "plt.plot(y)\n",
    "plt.title(f'between 0 and 1 (open internval), total: {len(y)}')\n",
    "y.sort()\n",
    "x = np.arange(len(y))\n",
    "\n",
    "plt.figure(figsize=(12, 12))\n",
    "\n",
    "# linear\n",
    "plt.subplot(221)\n",
    "plt.plot(x, y)\n",
    "plt.yscale('linear')\n",
    "plt.title('linear')\n",
    "plt.grid(True)\n",
    "\n",
    "# log\n",
    "plt.subplot(222)\n",
    "plt.plot(x, y)\n",
    "plt.yscale('log')\n",
    "plt.title('log')\n",
    "plt.grid(True)\n",
    "\n",
    "# symmetric log\n",
    "plt.subplot(223)\n",
    "plt.plot(x, y - y.mean())\n",
    "plt.yscale('symlog', linthresh=0.01)\n",
    "plt.title('symlog')\n",
    "plt.grid(True)\n",
    "\n",
    "# logit\n",
    "plt.subplot(224)\n",
    "plt.plot(x, y)\n",
    "plt.yscale('logit')\n",
    "plt.title('logit')\n",
    "plt.grid(True)\n",
    "\n",
    "\n"
   ]
  }
 ],
 "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.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
