{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data Visualization with MatPlotLib "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Introduction to MatPlotLib"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Matplotlib is one of the most powerful tools for data visualization in Python.\n",
    "\n",
    "Matplotlib is a multiplatform data visualization library built on NumPy arrays.\n",
    "\n",
    "The important feature of Matplotlib is its ability to play well with many operating systems and graphics backends.\n",
    "\n",
    "We can develop publication quality plots with just a few lines of code.\n",
    "\n",
    "The most important Visualizations in MatplotLib is Line Plot;Histogram;Box Plots;Bar Plot;Pie Chart;Scatter Plot;Quiver Plot;Violin Plot;Contour Plot;3D Plots etc."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  Importing Libraries and Datasets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Checking for supporting software"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install --user matplotlib\n",
    "! pip install --user numpy\n",
    "! pip install --user pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######  Importing Libraries "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.5.2\n",
      "1.23.5\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "print(pd.__version__)\n",
    "print(np.__version__)\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use Iris data set for the most of the plots."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Importing Datasets"
   ]
  },
  {
   "attachments": {
    "iris.jpg": {
     "image/jpeg": "[encoded data, 13180 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![iris.jpg](attachment:iris.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal length</th>\n",
       "      <th>sepal width</th>\n",
       "      <th>petal length</th>\n",
       "      <th>petal width</th>\n",
       "      <th>class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepal length  sepal width  petal length  petal width           class\n",
       "0             5.1          3.5           1.4          0.2     Iris-setosa\n",
       "1             4.9          3.0           1.4          0.2     Iris-setosa\n",
       "2             4.7          3.2           1.3          0.2     Iris-setosa\n",
       "3             4.6          3.1           1.5          0.2     Iris-setosa\n",
       "4             5.0          3.6           1.4          0.2     Iris-setosa\n",
       "..            ...          ...           ...          ...             ...\n",
       "145           6.7          3.0           5.2          2.3  Iris-virginica\n",
       "146           6.3          2.5           5.0          1.9  Iris-virginica\n",
       "147           6.5          3.0           5.2          2.0  Iris-virginica\n",
       "148           6.2          3.4           5.4          2.3  Iris-virginica\n",
       "149           5.9          3.0           5.1          1.8  Iris-virginica\n",
       "\n",
       "[150 rows x 5 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',\n",
    "                 sep=\",\",         \n",
    "                 names = ['sepal length','sepal width','petal length','petal width','class']) \n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######  Important Visualizations in MatplotLib\n",
    "\n",
    "Understand the anatomy of the plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Figure : Entire area for plotting the graph or figure\n",
    "2. Axes : Area where the plot is drawn\n",
    "3. Axis : Number lines in the Axes area\n",
    "4. Artist : Everything including figure, axes axis and other elements on the plot called Artist "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 1: Create a figure object with 5 inches width and 4 inches height and name it as fig. The below code returns figure object.\n",
    "#### syntax\n",
    "fig = plt.figure(figsize=(5,4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add the subplot and create the axes object. Here the subplot arguments resembles the position of the plot.\n",
    "<br>\n",
    "These might be three integers together or seperated by the comma.\n",
    "<br>\n",
    "First two integers represents rows, columns and the third integer represents the position of the subplot.\n",
    "\n",
    "#### syntax\n",
    "axes = fig.add_subplot(111)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 2\n",
    "<br> Setting Title and Axis Labels\n",
    "<br> Setting Title and Axis Labels\n",
    "<br> Set method can be used on created axes, to set various parameters such as xlabel, ylabel and title.\n",
    "#### syntax\n",
    "axes.set(title='Simple Plot',\n",
    "      xlabel='X-Axis', ylabel='Y-Axis',\n",
    "      xlim=(0, 8), ylim=(0,12))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 3:  <b>Draw the first simple plot </b>\n",
    "<br> Recommended to execute all the figure related settings at once."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13436 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "ax = fig.add_subplot(111)\n",
    "ax.set(title='First Plot', xlabel='X-Axis', ylabel='Y-Axis', xlim=(0, 5), ylim=(0,10))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Line Plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Draw a line plot.\n",
    "<br>\n",
    "Line plot can be drawn using axes.plot(x,y) where as x represents the X-axis elements versus y represents Y-axis elements.\n",
    "\n",
    "Note 1: Lets take x as some 10 random numbers\n",
    "<br>\n",
    "y as square(x) -> y = x**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "[ 0  1  4  9 16 25 36 49 64 81]\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(0,10)\n",
    "print(x)\n",
    "\n",
    "print('\\n')\n",
    "\n",
    "y = x**2\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 2: \n",
    "<b> Set the parameters </b>\n",
    "<br>Set the title as Quadratic Growth\n",
    "<br>\n",
    "X-axis label as x vals\n",
    "<br>Y-axis label as y vals\n",
    "<br>Limit the X axis at 0 to 10\n",
    "<br>Limit the Y axis at 0 to 90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33832 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set(title='Quadratic Growth', xlabel='x vals', ylabel='y vals', xlim=(0, 10), ylim=(0,90))\n",
    "axes.plot(x,y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 3: \n",
    "Add the label to the plot as y = x**2\n",
    "#### syntax\n",
    "label = 'y = x**2'\n",
    "<br>plt.legend([label])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 35512 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set(title='Quadratic Growth', xlabel='x vals', ylabel='y vals', xlim=(0, 10), ylim=(0,90))\n",
    "axes.plot(x,y,label = 'y = x**2')\n",
    "axes.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 4:  You can set the title, x label,y label and other properties using set_<parametername> as shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 35512 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set_title(\"Quadratic Growth\")\n",
    "axes.set_xlabel(\"x vals\")\n",
    "axes.set_ylabel('y vals')\n",
    "axes.set_xlim([0,10])\n",
    "axes.set_ylim([0,90])\n",
    "axes.plot(x,y, label='y = x**2')\n",
    "axes.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 5: \n",
    "You can set the parameters directly to the plot instead of the axes. \n",
    "<br>\n",
    "This will be apply to the entire plot.\n",
    "<br>It is preferred to set the parameters to the axes rather than to the plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32536 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "plt.title(\"Quadratic Growth\")\n",
    "plt.xlabel(\"x vals\")\n",
    "plt.ylabel('y vals')\n",
    "plt.xlim([0,10])\n",
    "plt.ylim([0,100])\n",
    "plt.plot(x,y, label='y == x**2')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 6: \n",
    "You can set other command parameters to plot functions such as \n",
    "<ol>\n",
    "<li> color - set the color of the line\n",
    "<li> marker - mark the data points with circle, upper triangle and other supported shapes\n",
    "<li> linestyle - dash, double dash\n",
    "<li> linewidth - specifies the width of the line ex: 3\n",
    "<li> and many other as needed\n",
    "</ol>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36992 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set_title(\"Quadratic Growth\")\n",
    "axes.set_xlabel(\"x vals\")\n",
    "axes.set_ylabel('y vals')\n",
    "axes.set_xlim([0,10])\n",
    "axes.set_ylim([0,90])\n",
    "axes.plot(x,y, label='y = x**2', color='green', linestyle=':', linewidth=3, marker='o', markersize=8, markerfacecolor='r')\n",
    "axes.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 7: \n",
    "You can draw multiple lines on the same axes. See the below example.\n",
    "<br>Draw another line with x vs y where y = x**3 and name the line as y = x**3.\n",
    "<br> Draw a  red solid line with green upper traiangle as marker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 44252 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "y3 = x**3\n",
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set_title(\" Growth\")\n",
    "axes.set_xlabel(\"x vals\")\n",
    "axes.set_ylabel('y vals')\n",
    "axes.set_xlim([0,10])\n",
    "axes.set_ylim([0,90])\n",
    "axes.plot(x,y, label='y = x**2', color='green', linestyle=':', linewidth=3, marker='o', markersize=8, markerfacecolor='r')\n",
    "axes.plot(x,y3, label='y = x**3', color='red', linewidth=3, marker='^', markersize=8, markerfacecolor='g')\n",
    "axes.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######  Histogram"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> Histogram </b>\n",
    "<br> Histograms normally shows the distribution of the data points. \n",
    "<br> To create a histogram we need to set the bins which will show the count of data points over the entire range of of the values.\n",
    "<br> Histogram is used for numerical data(continuous data points).\n",
    "<br> Histogram is also the simple form of Kernel Density Estimates.\n",
    "<br> Histograms shows the different patterns of the data distribution.\n",
    "    <ol>\n",
    "    <li>Symmetric, unimodal\n",
    "    <li>Skewed right\n",
    "    <li>Skewed left\n",
    "    <li>Bimodal\n",
    "    <li>Multimodal\n",
    "    <li>Symmetric    \n",
    "    </ol>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 1: \n",
    "Let us draw histogram on on Iris dataset sepal length feature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    150.000000\n",
       "mean       5.843333\n",
       "std        0.828066\n",
       "min        4.300000\n",
       "25%        5.100000\n",
       "50%        5.800000\n",
       "75%        6.400000\n",
       "max        7.900000\n",
       "Name: sepal length, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sepal_length = df['sepal length']\n",
    "\n",
    "sepal_length.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 1. A: Let us draw histogram on Iris dataset sepal length feature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 20416 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set(title=\"Distribution of Sepal Length\", ylabel='Count', xlabel='Sepal Length')\n",
    "axes.hist(sepal_length, bins=20, orientation='vertical')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 2: Let us draw histogram on Iris dataset petal length feature. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18180 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set(title=\"Distribution of Petal Length\", ylabel='Count', xlabel='Pepal Length')\n",
    "axes.hist(df['petal length'], bins=20, orientation='horizontal')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Box Plots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>Box Plots</b>\n",
    "<br>In descriptive statistics, a box plot is a method for graphically depicting groups of numerical data through their quartiles. Box plots may also have lines extending from the boxes (whiskers) indicating variability outside the upper and lower quartiles, hence the terms box-and-whisker plot and box-and-whisker diagram.\n",
    "<br>Using the box plot we can analyze the distribution of the data points around the median and can detect the outliers.\n",
    "<br> Box plot has mainly 5 elements\n",
    "<ol>\n",
    "    <li> Minimum\n",
    "    <li> Maximum\n",
    "    <li> Medain(Q2/50th percentile)\n",
    "    <li> First Quartile(Q1/25th percentile)\n",
    "    <li> Third Quartile(Q3/75th percentile)\n",
    "<ol>\n",
    "<br>Box is drawn between Q1 and Q3 quartiles.\n",
    "<br>Box shows the confidence around the median.\n",
    "\n",
    "<br>\n",
    "<b>Below Diagram explains the box plot.</b>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Box plot can be drawn using boxplot method.\n",
    "\n",
    "Note 1: \n",
    "<br> Draw a box plot for the Sepal length of Iris Data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22460 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set(title=\"Box plot of Iris Sepal Length\", xlabel='Sepal Length', ylabel='Length')\n",
    "axes.boxplot(sepal_length, notch=True,patch_artist=True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 2: \n",
    "<br>You can draw multiple box plots in the same figure. Pass the different variables as a list or sequence to the box plot with corresponding labels.\n",
    "<br>The below diagram shows the box plot for Sepal Length, Sepal Width, Petal Length and Petal Width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33464 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sepal_width = df['sepal width']\n",
    "petal_length = df['petal length']\n",
    "petal_width = df['petal width']\n",
    "fig = plt.figure(figsize=(8,6))\n",
    "ax = fig.add_subplot(111)\n",
    "ax.set(title=\"Box plot of Iris Measurements\", xlabel='Iris Measurements', ylabel='Length')\n",
    "ax.boxplot([sepal_length, sepal_width, petal_length, petal_width], \n",
    "           labels=[\"Sepal Length\", \"Sepal Width\", \"Petal Length\", \"Petal Width\"], notch=True, bootstrap=10000)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 3: \n",
    "<br>To draw the horizontal box plot set the vert as False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19596 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set(title=\"Box plot of Iris Sepal Length\", xlabel='Length', ylabel='Sepal Length')\n",
    "axes.boxplot(sepal_length, notch=True,patch_artist=True, vert=False)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 4: The below diagram shows the box plot for Sepal Length, Sepal Width, Petal Length and Petal Width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37072 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "ax = fig.add_subplot(111)\n",
    "ax.set(title=\"Box plot of Iris Measurements\", xlabel='Length', ylabel='Iris Measurement Types')\n",
    "ax.boxplot([sepal_length, sepal_width, petal_length, petal_width], \n",
    "           labels=[\"Sepal Length\", \"Sepal Width\", \"Petal Length\", \"Petal Width\"], \n",
    "           notch=True, bootstrap=10000, vert=False)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 5: \n",
    "<br> The above figure looks plain. Lets fill some colors to the box plots.\n",
    "<br> Fill with pink, lightgreen, lightblue, brown\n",
    "<br> Set patch_artist parameter to True\n",
    "<br> Iterate through all the boxes of the box plot and set the color and edge color as black.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36908 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "colors = [\"pink\", \"lightgreen\", \"lightblue\", \"brown\"]\n",
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set(title=\"Box plot of Iris Measurements\", xlabel='Length', ylabel='Iris Measurment Types')\n",
    "boxplot1 = axes.boxplot([sepal_length, sepal_width, petal_length, petal_width], \n",
    "           labels=[\"Sepal Length\", \"Sepal Width\", \"Petal Length\", \"Petal Width\"], \n",
    "           notch=True, bootstrap=10000, vert=False, patch_artist=True)\n",
    "\n",
    "for patch, color in zip(boxplot1['boxes'], colors):\n",
    "    patch.set_facecolor(color)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Bar Plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> Bar charts</b>\n",
    "\n",
    "Note 1:\n",
    "<br> Bar charts can be plotted using the function bar() for vertical graphs and barh() for horizontal graphs.\n",
    "<br> Color each Iris type class as orange, grey and black respectively.\n",
    "<br> Set the title as Avg. Sepal Width\n",
    "<br> Set the X-axis label as Iris Type\n",
    "<br> Set the Y-axis label as Mean Sepal Width\n",
    "<br> Set the X-axis ticks as per the Iris Class names\n",
    "<br> Set the X-ticks at 1, 2 and 3\n",
    "<br> Set the each bar width as 0.8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 20236 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "iriscolors = {'Iris-setosa':'red',\n",
    "             'Iris-versicolor':'blue', \n",
    "              'Iris-virginica':'yellow'}\n",
    "\n",
    "colorseq = [ iriscolors.get(cls) for cls in df['class'] ]\n",
    "\n",
    "iris_mean_sepal_width = [df[df['class']==cls]['sepal width'].max() for cls in iriscolors.keys()]\n",
    "\n",
    "plt.style.use('default')\n",
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set(title='Avg. Sepal Width', xlabel='Iris Type', ylabel='Mean Sepal Width')\n",
    "irisclassIndex = [1,2,3]\n",
    "irisclass = iriscolors.keys()\n",
    "axes.bar(irisclassIndex, iris_mean_sepal_width, color=['orange','grey','black'], width =0.8)\n",
    "axes.set_xticks(irisclassIndex)\n",
    "axes.set_xticklabels(irisclass)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 2: \n",
    "Draw the above bar graph horizontally with height defined as 0.75\n",
    "<br> Color each Iris type class as orange, grey and black respectively.\n",
    "<br> Set the title as Avg. Sepal Width\n",
    "<br> Set the Y-axis label as Iris Type\n",
    "<br> Set the X-axis label as Mean Sepal Width\n",
    "<br> Set the Y-axis ticks as per the Iris Class names\n",
    "<br> Set the Y-ticks at 1, 2 and 3\n",
    "<br> Set the each bar height as 0.75"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21120 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.style.use('default')\n",
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set(title='Avg. Sepal Width',\n",
    "      xlabel='Mean Sepal Width', ylabel='Iris Type')\n",
    "irisclassIndex = [1,2,3]\n",
    "irisclass = iriscolors.keys()\n",
    "axes.barh(irisclassIndex, iris_mean_sepal_width, color=['orange','grey','black'], height =0.75)\n",
    "axes.set_yticks(irisclassIndex)\n",
    "axes.set_yticklabels(irisclass)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 3: \n",
    "<b>Group Bar Charts</b>\n",
    "<br> We can draw multiple plots on the same axes.\n",
    "<br> The following barchart shows the mean Sepal length and Width for three different types of Iris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iris_mean_sepal_length = [df[df['class']==cls]['sepal length'].max() for cls in iriscolors.keys()]\n",
    "\n",
    "fig = plt.figure(figsize=(8,6))\n",
    "ax = fig.add_subplot(111)\n",
    "ax.set(title='Avg. Sepal Measurements',\n",
    "      xlabel='Iris Type', ylabel='Mean Sepal Measurements')\n",
    "irisclassIndex = [0.75, 2.25, 3.75]\n",
    "mean_sepal_width_index = [0.5, 2, 3.5]; mean_sepal_length_index = [1.0, 2.5, 4.0]\n",
    "ax.bar(mean_sepal_width_index, iris_mean_sepal_width, color='orange', width=0.5, edgecolor='black', label='Sepal Width')\n",
    "ax.bar(mean_sepal_length_index, iris_mean_sepal_length, color='blue', width=0.5, edgecolor='black', label='Sepal Length')\n",
    "ax.set_xticks(irisclassIndex)\n",
    "ax.set_xticklabels(irisclass)\n",
    "ax.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 4: \n",
    "Draw the above grouped bar chart horizontally.\n",
    "<br> Align the legend at lower right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "ax = fig.add_subplot(111)\n",
    "ax.set(title='Avg. Sepal Measurements',\n",
    "      xlabel='Mean Sepal Measurements', ylabel='Iris Type')\n",
    "irisclassIndex = [0.75, 2.25, 3.75]\n",
    "mean_sepal_width_index = [0.5, 2, 3.5]; mean_sepal_length_index = [1.0, 2.5, 4.0]\n",
    "ax.barh(mean_sepal_width_index, iris_mean_sepal_width, color='orange', height=0.5, edgecolor='black', label='Sepal Width')\n",
    "ax.barh(mean_sepal_length_index, iris_mean_sepal_length, color='blue', height=0.5, edgecolor='black', label='Sepal Length')\n",
    "ax.set_yticks(irisclassIndex)\n",
    "ax.set_yticklabels(irisclass)\n",
    "ax.legend(loc='lower right')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Pie Chart\n",
    "\n",
    "Plot a pie chart.\n",
    "\n",
    "\n",
    "Make a pie chart of array x. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 51996 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "explode = (0,0.2,0)\n",
    "axes.pie(x = df['class'].value_counts(), labels=irisclass, shadow=True, explode = explode, autopct='%1.1f%%' )\n",
    "axes.set_xlabel(\"Iris Classes\")\n",
    "axes.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Scatter Plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>Scatter plot</b> - Scatter plot can be drawn using axes.scatter() function. It plots the data points alone with the chosen marker style.\n",
    "<br> Scatter plot requires two variables x and y. Lets draw a scatter plot using the above defined x and y variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22644 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set_title(\" Growth\")\n",
    "axes.set_xlabel(\"x vals\")\n",
    "axes.set_ylabel('y vals')\n",
    "axes.set_xlim([0,10])\n",
    "axes.set_ylim([0,90])\n",
    "axes.scatter(x,y, label='y = x**2', color='green', marker='o')\n",
    "axes.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 1: \n",
    "\n",
    "Using scatter plot we can show different properties. \n",
    "\n",
    "Lets analyze the Iris dataset.\n",
    "<br> Define the color sequence for each class\n",
    "<br> Draw a scatter plot Petal Length vs Sepal Width with circular marker \n",
    "<br> Edge color black and set alpha value as 0.3 (alpha value add the water mark style to the markers)\n",
    "<br> Define the size of the marker using 100 times of petal width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.style.use('seaborn-whitegrid')\n",
    "fig = plt.figure(figsize=(8,6))\n",
    "axes = fig.add_subplot(111)\n",
    "axes.set_title(\"Petal Length vs Sepal Width\")\n",
    "axes.set_xlabel(\"Petal Length\")\n",
    "axes.set_ylabel('Sepal Width')\n",
    "axes.set_xlim([0,8])\n",
    "axes.set_ylim([0,5])\n",
    "\n",
    "axes.scatter(df['petal length'],df['sepal width'],\n",
    "             c= colorseq, marker='o', edgecolors='black', \n",
    "             s=df['petal width']*100, alpha=0.3, cmap='viridis')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Grids, Multiplots, & Subplots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Draw multiple plots using the subplot.\n",
    "<br>\n",
    "Syntax:\n",
    "<br><b> subplot(nrows,ncols, index)</b>\n",
    "<br>nrows represents the number of rows.\n",
    "<br>ncols represents the number of columns.\n",
    "<br>index indicates the position or the plot number of the diagram.\n",
    "<br>\n",
    "<br>\n",
    "\n",
    "\n",
    "Note 1: \n",
    "\n",
    "Draw a plot with 4 subplots in it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19216 characters]\n",
      "text/plain": [
       "<Figure size 600x400 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(6,4))\n",
    "axes1 = plt.subplot(2, 2, 1, title='Plot1')\n",
    "axes2 = plt.subplot(2, 2, 2, title='Plot2')\n",
    "axes3 = plt.subplot(2, 2, 3, title='Plot3')\n",
    "axes4 = plt.subplot(2, 2, 4, title='Plot4')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 2: \n",
    "\n",
    "Draw a multiplot with the first plot occupies completely the first row and the other 2 plots at 3rd position and 4th position resprectively."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23356 characters]\n",
      "text/plain": [
       "<Figure size 1000x800 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(10,8))\n",
    "axes1 = plt.subplot(2, 2, (1,2), title='Plot1')\n",
    "axes3 = plt.subplot(2, 2, 3, title='Plot2')\n",
    "axes4 = plt.subplot(2, 2, 4, title='Plot3')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 3: \n",
    "Draw a multiplot with the first plot occupies completely the first column and the other 2 plots at 2nd position and 4th position resprectively."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26412 characters]\n",
      "text/plain": [
       "<Figure size 1000x800 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(10,8))\n",
    "axes1 = plt.subplot(2, 2, (1,3), title='Plot1')\n",
    "axes3 = plt.subplot(2, 2, 2, title='Plot2')\n",
    "axes4 = plt.subplot(2, 2, 4, title='Plot3')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note 4:  \n",
    "Let's draw the same multiple plots using subplots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 129824 characters]\n",
      "text/plain": [
       "<Figure size 1000x800 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "fig = plt.figure(figsize=(10,8))\n",
    "axes1 = plt.subplot(2, 2, (1,2), title='Plot1') # Occupies the first row and two columns\n",
    "axes2 = plt.subplot(2, 2, 3, title='Plot2') # Occupies the second row and first column\n",
    "axes3 = plt.subplot(2, 2, 4, title='Plot3') # Occupies the second row and second column\n",
    "\n",
    "# Draw a scatter plot that we created in the earlier steps\n",
    "axes1.set_title(\"Petal Length vs Sepal Width\")\n",
    "axes1.set_xlabel(\"Petal Length\")\n",
    "axes1.set_ylabel('Sepal Width')\n",
    "axes1.set_xlim([0,8])\n",
    "axes1.set_ylim([0,5])\n",
    "axes1.scatter(df['petal length'],df['sepal width'],\n",
    "             c= colorseq, marker='o', edgecolors='black', \n",
    "             s=df['petal width']*100, alpha=0.3, cmap='viridis')\n",
    "\n",
    "# Draw the box plot for Sepal Length, Sepal Width, Petal Length and Petal Width\n",
    "axes2.boxplot([sepal_length, sepal_width, petal_length, petal_width], \n",
    "           labels=[\"Sepal Length\", \"Sepal Width\", \"Petal Length\", \"Petal Width\"], \n",
    "           notch=True, bootstrap=10000, vert=False, patch_artist=True)\n",
    "axes2.set(title=\"Box plot of Iris Measurements\",\n",
    "      xlabel='Length', ylabel='Iris Measurment Types')\n",
    "\n",
    "# Draw a bar plot representing the mean sepal width for different Iris Types.\n",
    "axes3.set(title='Avg. Sepal Width',\n",
    "      xlabel='Iris Type', ylabel='Mean Sepal Width')\n",
    "axes3.bar(irisclassIndex, iris_mean_sepal_width, color=['orange','grey','black'], width =0.8)\n",
    "axes3.set_xticks(irisclassIndex)\n",
    "axes3.set_xticklabels(irisclass)\n",
    "\n",
    "# Set the Multiple Plots uisng Grids as super title of the multiple plots\n",
    "plt.suptitle(\"Multiple Plots using Grids\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
