{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!DOCTYPE html>\n",
    "<html>\n",
    "<body>\n",
    "<div align=\"center\">\n",
    "<h3>Prepared by Asif Bhat</h3>\n",
    "    \n",
    "<h1>Data Visualization With Matplotlib</h1>\n",
    "\n",
    "<h3>Follow Me on - <a href=\"https://www.linkedin.com/in/asif-bhat/\">LinkedIn</a>&nbsp; <a href=\"https://mobile.twitter.com/_asifbhat_\">Twitter</a>&nbsp; <a href=\"https://www.instagram.com/datasciencescoop/?hl=en\">Instagram</a>&nbsp; <a href=\"https://www.facebook.com/datasciencescoop/\">Facebook</a></h3>\n",
    "</div>\n",
    "\n",
    "</div>\n",
    "</body>\n",
    "</html>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Graph Styling\n",
    "# https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html\n",
    "plt.style.use('seaborn-darkgrid')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bar Graphs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 288,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 5692 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#Simple Bar Chart\n",
    "id1 = np.arange(1,10)\n",
    "score = np.arange(20,110,10)\n",
    "plt.bar(id1,score)\n",
    "plt.xlabel('Student ID')\n",
    "plt.ylabel('Score')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 289,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 6796 characters]\n",
      "text/plain": [
       "<Figure size 576x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Changing color of the bar chart\n",
    "id1 = np.arange(1,10)\n",
    "score = np.arange(20,110,10)\n",
    "plt.figure(figsize=(8,5)) # Setting the figure size\n",
    "ax = plt.axes()\n",
    "ax.set_facecolor(\"#ECF0F1\") # Setting the background color by specifying the HEX Code\n",
    "plt.bar(id1,score,color = '#FFA726')\n",
    "plt.xlabel(r'$Student $ $ ID$')\n",
    "plt.ylabel(r'$Score$')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 290,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 8556 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#Plotting multiple sets of data\n",
    "x1= [1,3,5,7] \n",
    "x2=[2,4,6,8]\n",
    "y1 = [7,7,7,7]\n",
    "y2= [17,18,29,40]\n",
    "plt.figure(figsize=(8,6))\n",
    "ax = plt.axes()\n",
    "ax.set_facecolor(\"white\")\n",
    "plt.bar(x1,y1,label = \"First\",color = '#42B300') # First set of data\n",
    "plt.bar(x2,y2,label = \"Second\",color = '#94E413') # Second set of data\n",
    "plt.xlabel('$X$')\n",
    "plt.ylabel('$Y$')\n",
    "plt.title ('$Bar $ $ Chart$')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 291,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 5056 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Horizontal Bar Chart\n",
    "Age = [28,33,43,45,57]\n",
    "Name = [\"Asif\", \"Steve\", 'John', \"Ravi\", \"Basit\"]\n",
    "plt.barh(Name,Age, color =\"yellowgreen\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 293,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 293,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 4816 characters]\n",
      "text/plain": [
       "<Figure size 576x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Changing the width of Bars\n",
    "num1 = np.array([1,3,5,7,9])\n",
    "num2 = np.array([2,4,6,8,10])\n",
    "plt.figure(figsize=(8,4))\n",
    "plt.bar(num1, num1**2, width=0.2 , color = '#FF6F00')\n",
    "plt.bar(num2, num2**2, width=0.2 , color = '#FFB300')\n",
    "plt.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 294,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 294,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 9140 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Displaying values at the top of vertical bars\n",
    "num1 = np.array([1,3,5,7,9])\n",
    "num2 = np.array([2,4,6,8,10])\n",
    "plt.figure(figsize=(10,6))\n",
    "plt.bar(num1, num1**2, width=0.3 , color = '#FF6F00')\n",
    "plt.bar(num2, num2**2, width=0.3 , color = '#FFB300')\n",
    "for x,y in zip(num1,num1**2):\n",
    "    plt.text(x, y+0.05, '%d' % y, ha='center' , va= 'bottom')\n",
    "for x,y in zip(num2,num2**2):\n",
    "    plt.text(x, y+0.05, '%d' % y, ha='center' , va= 'bottom')\n",
    "plt.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16952 characters]\n",
      "text/plain": [
       "<Figure size 1152x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.arange(1,21)\n",
    "plt.figure(figsize=(16,8))\n",
    "y1 = np.random.uniform(0.1,0.7,20)\n",
    "y2 = np.random.uniform(0.1,0.7,20)\n",
    "\n",
    "\n",
    "plt.bar(x, +y1, facecolor='#C0CA33', edgecolor='white') #specify edgecolor by name\n",
    "plt.bar(x, -y2, facecolor='#FF9800', edgecolor='white')\n",
    "\n",
    "for x,y in zip(x,y1):\n",
    "    plt.text(x, y+0.05, '%.2f' % y, ha='center' , va= 'bottom', fontsize = 10)\n",
    "    \n",
    "plt.xlim(0,21)\n",
    "plt.ylim(-1.25,+1.25)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stacked Vertical Bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 296,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12068 characters]\n",
      "text/plain": [
       "<Figure size 360x504 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.style.use('seaborn-darkgrid')\n",
    "x1= ['Asif','Basit','Ravi','Minil'] \n",
    "y1= [17,18,29,40]\n",
    "y2 = [20,21,22,23]\n",
    "plt.figure(figsize=(5,7))\n",
    "plt.bar(x1,y1,label = \"Open Tickets\",width = 0.5,color = '#FF6F00')\n",
    "plt.bar(x1,y2,label = \"Closed Tickets\",width = 0.5 ,bottom = y1 , color = '#FFB300')\n",
    "plt.xlabel('$X$')\n",
    "plt.ylabel('$Y$')\n",
    "plt.title ('$Bar $ $ Chart$')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14160 characters]\n",
      "text/plain": [
       "<Figure size 360x504 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.style.use('seaborn-darkgrid')\n",
    "x1= ['Asif','Basit','Ravi','Minil'] \n",
    "y1= np.array([17,18,29,40])\n",
    "y2 =np.array([20,21,22,23])\n",
    "y3 =np.array([5,9,11,12])\n",
    "plt.figure(figsize=(5,7))\n",
    "plt.bar(x1,y1,label = \"Open Tickets\",width = 0.5,color = '#FF6F00')\n",
    "plt.bar(x1,y2,label = \"Closed Tickets\",width = 0.5 ,bottom = y1 , color = '#FFB300')\n",
    "plt.bar(x1,y3,label = \"Cancelled Tickets\",width = 0.5 ,bottom = y1+y2 , color = '#F7DC6F')\n",
    "plt.xlabel('$X$')\n",
    "plt.ylabel('$Y$')\n",
    "plt.title ('$Bar $ $ Chart$')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Grouped Bar Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15056 characters]\n",
      "text/plain": [
       "<Figure size 504x648 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3])"
      ]
     },
     "execution_count": 298,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Grouped Bar Chart\n",
    "\n",
    "plt.figure(figsize=(7,9))\n",
    "\n",
    "# set width of bar\n",
    "barWidth = 0.25\n",
    " \n",
    "# set height of bar\n",
    "y1= np.array([17,18,29,40])\n",
    "y2 =np.array([20,21,22,23])\n",
    "y3 =np.array([5,9,11,12])\n",
    " \n",
    "# Set position of bar on X axis\n",
    "pos1 = np.arange(len(y1))\n",
    "pos2 = [x + barWidth for x in pos1]\n",
    "pos3 = [x + barWidth for x in pos2]\n",
    "\n",
    "# Make the plot\n",
    "plt.bar(pos1, y1, color='#FBC02D', width=barWidth, label='Open')\n",
    "plt.bar(pos2, y2, color='#F57F17', width=barWidth, label='Closed')\n",
    "plt.bar(pos3, y3, color='#E65100', width=barWidth, label='Cancelled')\n",
    "\n",
    "# Add xticks on the middle of the group bars\n",
    "plt.xlabel('Assignee', fontweight='bold')\n",
    "plt.ylabel('Number of Tickets', fontweight='bold')\n",
    "plt.xticks([i + barWidth for i in range(len(y1))], ['Asif', 'Basit', 'Ravi', 'Minil'])\n",
    "\n",
    "# Create legend & Show graphic\n",
    "plt.legend()\n",
    "plt.show()\n",
    "np.arange(len(y1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stacked Vertical Bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 11440 characters]\n",
      "text/plain": [
       "<Figure size 576x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.style.use('seaborn-darkgrid')\n",
    "x1= ['Asif','Basit','Ravi','Minil'] \n",
    "y1= [17,18,29,40]\n",
    "y2 = [20,21,22,23]\n",
    "plt.figure(figsize=(8,5))\n",
    "plt.barh(x1,y1,label = \"Open Tickets\",color = '#FF6F00')\n",
    "plt.barh(x1,y2,label = \"Closed Tickets\", left = y1 , color = '#FFB300')\n",
    "plt.xlabel('$X$')\n",
    "plt.ylabel('$Y$')\n",
    "plt.title ('$Bar $ $ Chart$')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Displaying values in Bar Charts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14556 characters]\n",
      "text/plain": [
       "<Figure size 360x504 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Displaying values in the stacked vertical bars using plt.text()\n",
    "plt.style.use('seaborn-darkgrid')\n",
    "x1= ['Asif','Basit','Ravi','Minil'] \n",
    "y1= [17,18,29,40]\n",
    "y2 = [20,21,22,23]\n",
    "plt.figure(figsize=(5,7))\n",
    "plt.bar(x1,y1,label = \"Open Tickets\",width = 0.5,color = '#FF6F00')\n",
    "plt.bar(x1,y2,label = \"Closed Tickets\",width = 0.5 ,bottom = y1 , color = '#FFB300')\n",
    "plt.xlabel('$X$')\n",
    "plt.ylabel('$Y$')\n",
    "plt.title ('$Bar $ $ Chart$')\n",
    "for x,y in zip(x1,y1):\n",
    "    plt.text(x, y-10, '%d' % y, ha='center' , va= 'bottom')\n",
    "\n",
    "for x,y,z in zip(x1,y2,y1):\n",
    "    plt.text(x, y+z-10, '%d' % y, ha='center' , va= 'bottom')\n",
    "    \n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13948 characters]\n",
      "text/plain": [
       "<Figure size 576x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Displaying values in the stacked horizontal bars using plt.text()\n",
    "plt.style.use('seaborn-darkgrid')\n",
    "x1= ['Asif','Basit','Ravi','Minil'] \n",
    "y1= [17,18,29,40]\n",
    "y2 = [20,21,22,23]\n",
    "plt.figure(figsize=(8,5))\n",
    "plt.barh(x1,y1,label = \"Open Tickets\",color = '#FF6F00')\n",
    "plt.barh(x1,y2,label = \"Closed Tickets\", left = y1 , color = '#FFB300')\n",
    "plt.xlabel('$X$')\n",
    "plt.ylabel('$Y$')\n",
    "\n",
    "for x,y in zip(x1,y1):\n",
    "    plt.text(y-10, x, '%d' % y, ha='center' , va= 'bottom')\n",
    "    \n",
    "for x,y,z in zip(x1,y2,y1):\n",
    "    plt.text(y+z-10, x, '%d' % y, ha='center' , va= 'bottom')\n",
    "    \n",
    "plt.title ('$Bar $ $ Chart$')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21180 characters]\n",
      "text/plain": [
       "<Figure size 504x648 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Displaying values at the top of the Grouped Bar Chart using plt.text()\n",
    "plt.figure(figsize=(7,9))\n",
    "\n",
    "# set width of bar\n",
    "barWidth = 0.25\n",
    " \n",
    "# set height of bar\n",
    "y1= np.array([17,18,29,40])\n",
    "y2 =np.array([20,21,22,23])\n",
    "y3 =np.array([5,9,11,12])\n",
    " \n",
    "# Set position of bar on X axis\n",
    "pos1 = np.arange(len(y1))\n",
    "pos2 = [x + barWidth for x in pos1]\n",
    "pos3 = [x + barWidth for x in pos2]\n",
    "\n",
    "# Make the plot\n",
    "plt.bar(pos1, y1, color='#FBC02D', width=barWidth, label='Open')\n",
    "plt.bar(pos2, y2, color='#F57F17', width=barWidth, label='Closed')\n",
    "plt.bar(pos3, y3, color='#E65100', width=barWidth, label='Cancelled')\n",
    "\n",
    "# Add xticks on the middle of the group bars\n",
    "plt.xlabel('Assignee', fontweight='bold')\n",
    "plt.ylabel('Number of Tickets', fontweight='bold')\n",
    "plt.xticks([i + barWidth for i in range(len(y1))], ['Asif', 'Basit', 'Ravi', 'Minil'])\n",
    "\n",
    "for x,y in zip(pos1,y1):\n",
    "    plt.text(x, y, '%d' % y, ha='center' , va= 'bottom')\n",
    "    \n",
    "for x,y in zip(pos2,y2):\n",
    "    plt.text(x, y, '%d' % y, ha='center' , va= 'bottom')\n",
    "\n",
    "for x,y in zip(pos3,y3):\n",
    "    plt.text(x, y, '%d' % y, ha='center' , va= 'bottom')\n",
    "\n",
    "plt.title ('$Grouped $  $ Bar $  $ Chart$')\n",
    "\n",
    "# Create legend & Show graphic\n",
    "plt.legend()\n",
    "plt.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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
