{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 65648 characters]\n",
      "text/plain": [
       "<Figure size 504x180 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.patches as mpatches\n",
    "from matplotlib.colors import colorConverter as cc\n",
    "import numpy as np\n",
    " \n",
    "def plot_mean_and_CI(mean, lb, ub, color_mean=None, color_shading=None):\n",
    "    # plot the shaded range of the confidence intervals\n",
    "    plt.fill_between(range(mean.shape[0]), ub, lb,\n",
    "                     color=color_shading, alpha=.5)\n",
    "    # plot the mean on top\n",
    "    plt.plot(mean, color_mean)\n",
    " \n",
    "# generate 3 sets of random means and confidence intervals to plot\n",
    "mean0 = np.random.random(50)\n",
    "ub0 = mean0 + np.random.random(50) + .5\n",
    "lb0 = mean0 - np.random.random(50) - .5\n",
    " \n",
    "mean1 = np.random.random(50) + 2\n",
    "ub1 = mean1 + np.random.random(50) + .5\n",
    "lb1 = mean1 - np.random.random(50) - .5\n",
    " \n",
    "mean2 = np.random.random(50) -1\n",
    "ub2 = mean2 + np.random.random(50) + .5\n",
    "lb2 = mean2 - np.random.random(50) - .5\n",
    " \n",
    "# plot the data\n",
    "fig = plt.figure(1, figsize=(7, 2.5))\n",
    "plot_mean_and_CI(mean0, ub0, lb0, color_mean='k', color_shading='k')\n",
    "plot_mean_and_CI(mean1, ub1, lb1, color_mean='b', color_shading='b')\n",
    "plot_mean_and_CI(mean2, ub2, lb2, color_mean='g--', color_shading='g')\n",
    " \n",
    "class LegendObject(object):\n",
    "    def __init__(self, facecolor='red', edgecolor='white', dashed=False):\n",
    "        self.facecolor = facecolor\n",
    "        self.edgecolor = edgecolor\n",
    "        self.dashed = dashed\n",
    " \n",
    "    def legend_artist(self, legend, orig_handle, fontsize, handlebox):\n",
    "        x0, y0 = handlebox.xdescent, handlebox.ydescent\n",
    "        width, height = handlebox.width, handlebox.height\n",
    "        patch = mpatches.Rectangle(\n",
    "            # create a rectangle that is filled with color\n",
    "            [x0, y0], width, height, facecolor=self.facecolor,\n",
    "            # and whose edges are the faded color\n",
    "            edgecolor=self.edgecolor, lw=3)\n",
    "        handlebox.add_artist(patch)\n",
    " \n",
    "        # if we're creating the legend for a dashed line,\n",
    "        # manually add the dash in to our rectangle\n",
    "        if self.dashed:\n",
    "            patch1 = mpatches.Rectangle(\n",
    "                [x0 + 2*width/5, y0], width/5, height, facecolor=self.edgecolor,\n",
    "                transform=handlebox.get_transform())\n",
    "            handlebox.add_artist(patch1)\n",
    " \n",
    "        return patch\n",
    " \n",
    "bg = np.array([1, 1, 1])  # background of the legend is white\n",
    "colors = ['black', 'blue', 'green']\n",
    "# with alpha = .5, the faded color is the average of the background and color\n",
    "colors_faded = [(np.array(cc.to_rgb(color)) + bg) / 2.0 for color in colors]\n",
    " \n",
    "plt.legend([0, 1, 2], ['Data 0', 'Data 1', 'Data 2'],\n",
    "           handler_map={\n",
    "               0: LegendObject(colors[0], colors_faded[0]),\n",
    "               1: LegendObject(colors[1], colors_faded[1]),\n",
    "               2: LegendObject(colors[2], colors_faded[2], dashed=True),\n",
    "            })\n",
    " \n",
    "plt.title('Example mean and confidence interval plot')\n",
    "plt.tight_layout()\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.37747772, 0.07693753, 0.1940634 , 0.15176569, 0.14293999,\n",
       "       0.60894227, 0.09985447, 0.41118113, 0.68274936, 0.85285374,\n",
       "       0.4541564 , 0.63333913, 0.35620657, 0.51059929, 0.85266049,\n",
       "       0.23237531, 0.23283028, 0.47911222, 0.40544178, 0.81126309,\n",
       "       0.86811543, 0.35334637, 0.86403165, 0.86593378, 0.25916208,\n",
       "       0.21492518, 0.92107401, 0.32022366, 0.93038498, 0.4327189 ,\n",
       "       0.24912534, 0.77540019, 0.13939365, 0.79231252, 0.65351859,\n",
       "       0.11080199, 0.48425663, 0.7100898 , 0.02117045, 0.41127722,\n",
       "       0.16392522, 0.32550661, 0.95452695, 0.42164106, 0.22468096,\n",
       "       0.65491521, 0.36794668, 0.78792547, 0.17727132, 0.82141194])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
