{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# matplotlib:  the chart plotting library"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<U>Notes if you are using Jupyter Notebook</U>:  to call <B>exit()</B> from a notebook, please use <B>sys.exit()</B> (requires <B>import sys</B>); if a strange error occurs, it may be because Jupyter retains variables from all executed cells.  To reset the notebook's variables, click 'Restart Kernel' (the circular arrow) -- this will not undo any text changes.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Documentation and Study"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Matplotlib documentation can be found here:  <A HREF=\"http://matplotlib.org/\">http://matplotlib.org/</A>\n",
    "\n",
    "A very good rundown of features is in the <A HREF=\"http://opencarts.org/sachlaptrinh/pdf/28232.pdf\">Python for Data Analysis 2nd Edition PDF, Chapter 9</A>\n",
    "\n",
    "A clear tutorial on the central plotting function <B>pyplot</B> (part of which was used for this presentation) can be found here:  <A HREF=\"https://matplotlib.org/users/pyplot\\_tutorial.html\">https://matplotlib.org/users/pyplot\\_tutorial.html</A>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## importing matplotlib and seaborn; using Jupyter notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  Get started with charting tools.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt   # access matplotlib\n",
    "import seaborn as sns             # access seaborn\n",
    "\n",
    "sns.set()                         # apply seaborn default styles and scaling"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<B>matplotlib</B> is the standard Python charting library, tightly integrated with pandas.\n",
    "\n",
    "<B>seaborn</B> is a library built on top of matplotlib, providing support for statistical analysis and offering attractive styles.  <B>import seaborn</B> and <B>sns.set()</B> change matplotlib styles.\n",
    "\n",
    "<B>%matplotlib inline</B> is a Jupyter notebook \"magic\" command that will allow Notebook to display chart plots in the notebook; otherwise, we would need to save our charts to an image file."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting from matplotlib with plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  <B>plt</B> can be used to control the \"current\" chart (or <I>subplot</I>)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This approach uses the \"default\" <I><B>subplot</B></I> and <I><B>figure</B></I> objects.\n",
    "\n",
    "A <I><B>subplot</B></I> is a chart:  line, bar, scatter, etc.\n",
    "\n",
    "A <I><B>figure</B></I> is an overall image:  it may contain multiple subplots.  \n",
    "\n",
    "<B>SPECIAL NOTE</B> in many online examples, \"subplots\" (actual line or bar charts) are referred to as \"axes\" and subplot objects are named <B>ax</B>.  This is to be distinguished from chart axes (x and y).  Examples in this notebook follow this convention in order to condition your thinking to existing examples online.  <B>Remember, <I>ax</I> usually refers to a <I>subplot</I></B>.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "line_1_data = [1, 2, 4, 6,  8, 10, 12,  14,  16]\n",
    "line_2_data = [1, 2, 4, 8, 16, 32, 64, 128, 256]\n",
    "\n",
    "# plot lines to current subplot\n",
    "plt.plot(line_1_data, label='constant')\n",
    "plt.plot(line_2_data, 'r--', label='geometric')  # red, dashed\n",
    "\n",
    "\n",
    "# TITLE\n",
    "plt.title('A Very Special Relationship')\n",
    "\n",
    "# AXIS LABELS\n",
    "plt.xlabel('time')\n",
    "plt.ylabel('level')\n",
    "\n",
    "# TICKS\n",
    "plt.xticks(ticks=[0, 2, 4, 6, 8])\n",
    "plt.yticks(ticks=[0, 50, 100, 150, 200, 250])\n",
    "\n",
    "# GRIDLINES\n",
    "plt.grid(True)\n",
    "\n",
    "# LEGEND\n",
    "plt.legend();\n",
    "\n",
    "plt.savefig('chart.png')    # save figure to image file"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<br>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.clf()                             # clear the figure\n",
    "plt.plot([1, 5, 3, 7, 5, 9], 'g-')    # green, solid - see line styles\n",
    "\n",
    "plt.savefig('chart.png')     # save figure to image file"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting from matplotlib with figure and subplot objects"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### This approach allows for multiple subplots (charts) within a figure (chart image).   \n",
    "\n",
    "<B>SPECIAL NOTE</B> in many online examples, \"subplots\" (actual line or bar charts) are referred to as \"axes\" and subplot objects are named <B>ax</B>.  This is to be distinguished from chart axes (x and y).  Examples in this notebook follow this convention in order to condition your thinking to existing examples online.  <B>Remember, <I>ax</I> usually refers to a <I>subplot</I></B>.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig = plt.figure()         # \"figure\" object representing overall chart image\n",
    "ax = fig.add_subplot()     # \"subplot\" object representing a chart\n",
    "\n",
    "ax.plot([1, 2, 3, 4, 5], label='constant')   # plot a line on the subplot\n",
    "ax.plot([1, 4, 6, 8, 10], label='doubling')  # plot a line on the subplot\n",
    "\n",
    "# TITLE (see 'text' for more options)\n",
    "ax.title.set_text('Awesome Chart!!')\n",
    "\n",
    "# AXIS LABELS\n",
    "ax.set_xlabel('time')\n",
    "ax.set_ylabel('level')\n",
    "\n",
    "# TICK VALUES\n",
    "ax.set_xticks(ticks=[0, 1, 2, 3, 4])\n",
    "ax.set_yticks(ticks=[0, 2, 4, 6, 8, 10])\n",
    "\n",
    "# TICK LABELS\n",
    "ax.set_xticklabels(['12:00', '12:30', '1:00', '1:30', '2:00'])\n",
    "\n",
    "# GRIDLINES\n",
    "plt.grid(True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Saving multiple plots within a figure"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### We may prefer to work with the <B>figure</B> object in order to create multiple plots within a figure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig = plt.figure()              # 'figure' representing overall chart\n",
    "ax1 = fig.add_subplot(2, 1, 1)  # specifies  2 subplots, this one at row 1, col 1\n",
    "ax2 = fig.add_subplot(2, 1, 2)  # reiterates 2 subplots, this one at row 1, col 2\n",
    "\n",
    "line_1_data =  [1, 2, 3,  4,  5,  6,  7,  8,  9]\n",
    "doubled_data = [1, 2, 6,  8, 10, 12, 14, 16, 18]\n",
    "squared_data = [1, 2, 9, 16, 25, 36, 49, 64, 81]\n",
    "\n",
    "ax1.plot(line_1_data, label='constant')   # plot a line on subplot 1\n",
    "ax1.plot(doubled_data, label='doubling')  # same\n",
    "\n",
    "ax2.plot(line_1_data, label='constant')   # plot a line on subplot 2\n",
    "ax2.plot(squared_data, label='squaring')  # same\n",
    "\n",
    "# AXIS LABELS\n",
    "ax1.set_xlabel('time')\n",
    "ax1.set_ylabel('level')\n",
    "\n",
    "ax1.set_xlabel('time')\n",
    "ax1.set_ylabel('level')\n",
    "\n",
    "# TICKS\n",
    "ax1.set_xticks(ticks=[0, 1, 2, 3, 4, 5, 6, 7, 8])\n",
    "ax1.set_yticks(ticks=[0, 5, 10, 15, 20])\n",
    "\n",
    "ax2.set_xticks(ticks=[0, 1, 2, 3, 4, 5, 6, 7, 8])\n",
    "ax2.set_yticks(ticks=[0, 20, 40, 60, 80, 100])\n",
    "\n",
    "\n",
    "# LEGEND\n",
    "ax1.legend()\n",
    "ax2.legend();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## plotting from pandas DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  pandas' tight integration with matplotlib allows for easy chart plotting from pandas DataFrames and Series.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df = pd.DataFrame({ 'constant': [1, 2, 3,  4,  5,  6,  7,  8,  9],\n",
    "                    'doubled':  [1, 2, 6,  8, 10, 12, 14, 16, 18] },\n",
    "                    index=[0, 1, 2, 3, 4, 5, 6, 7, 8])\n",
    "\n",
    "df.plot();          # default - 'line'\n",
    "# df.plot.line()      # same; also df.plot.bar(), df.plot.barh(), etc.\n",
    "\n",
    "print(df)\n",
    "                    #    constant   doubled\n",
    "                    # 0     1          1\n",
    "                    # 1     2          2\n",
    "                    # 2     3          6\n",
    "                    # 3     4          8\n",
    "                    # 4     5         10\n",
    "                    # 5     6         12\n",
    "                    # 6     7         14\n",
    "                    # 7     8         16\n",
    "                    # 8     9         18\n",
    "\n",
    "\n",
    "plt.savefig('chart.tiff')    # save \"current\" figure to image file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({ 'constant': [1, 2, 3,  4,  5,  6,  7,  8,  9],\n",
    "                    'squared':  [1, 2, 9, 16, 25, 36, 49, 64, 81] },\n",
    "                    index=[0, 1, 2, 3, 4, 5, 6, 7, 8])\n",
    "\n",
    "ax = df.plot(title='Awesome!',\n",
    "             xticks=[1, 3, 5, 7, 9],        # default is index\n",
    "#             use_index=True,\n",
    "             yticks=[20, 40, 60, 80, 100],\n",
    "             fontsize=15,                   # TICK font\n",
    "             rot=45,                        # TICK rotation\n",
    "             xlim=(0, 10),                  # X AXIS SCALE limits\n",
    "             ylim=(0, 120),                 # Y AXIS SCALE limits\n",
    "             xlabel='Joe',\n",
    "             ylabel='Pete',\n",
    "             legend=True,                   # LEGEND:    True is default\n",
    "             grid=True,                     # GRIDLINES: True is default\n",
    "             style=['r-', 'b--']            # LINE STYLES list (can also be dict)\n",
    "     );\n",
    "\n",
    "# USING the SUBPLOT OBJECT here, to set y tick labels\n",
    "ax.set_yticklabels(['a', 'b', 'c', 'd', 'e']);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<H4>Retrieving the 'subplot' object from a DataFrame plot</H4>\n",
    "\n",
    "We may need to change styles on a DataFrame plot - all we need to do is retrieve the subplot object and change its attributes - even if <B>.plot()</B> has been called.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = df.plot.line()\n",
    "fig = ax.get_figure()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting tick intervals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  If matplotlib's default intervals are not suitable, some manual intervention is required."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Matplotlib attempts to set the most reasonable tick intervals but sometimes screws it up.  We may wish to set them directly; we may have to calculate the range and spacing ourselves and set them manually."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({ 'constant': [1, 2, 3,  4,  5,  6,  7,  8,  9],\n",
    "                    'squared':  [1, 2, 9, 16, 25, 36, 49, 64, 81] },\n",
    "                    index=[0, 1, 2, 3, 4, 5, 6, 7, 8])\n",
    "\n",
    "ax = df.plot(title='Awesome!',\n",
    "     yticks=range(20, 101, 20),     # [20, 40, 60, 80, 100]\n",
    "#     xticks=range(11),             # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "     xticks=np.linspace(0, 10, 20, endpoint=False));  # [0, 0.5, 1..10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can always set the specific numbers we wish to see and they will be duly applied:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "yticks=range(20, 101, 20),     # [20, 40, 60, 80, 100]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes however we may want to set a range, with or without a step:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# xticks=range(0, 11)               # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "xticks=range(0, 11, 2),           # [0, 2, 4, 6, 8, 10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To choose a fractional interval in a range, use <B>np.linspace()</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.linspace(0, 10, 20, endpoint=False)  # [0, 0.5, 1, 1.5 ... 9]\n",
    "\n",
    "np.linspace(1, 10, 19, endpoint=True)   # [1, 1.5, 2, 2.5 ... 10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above <B>linspace()</B> arguments read \"20 data points between 0 and 9 (not including 10)\" and \"19 data points between 1 and 10 (including 10)\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The range may also be based on the available data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "yticks=range(0, len(df))                    # [0..# of df values]\n",
    "xticks=range(min(df.index), max(df.index))  # from highest to lowest in index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows how an index set from a datetime64 column intelligently sets x axis tick labels (not always guaranteed):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('data/weather_newyork.csv')\n",
    "df.EST = df.EST.astype('datetime64[ns]')\n",
    "df = df.set_index('EST')                   # now a DateTimeIndex\n",
    "ax = df['Mean TemperatureF'].plot(rot=30,\n",
    "                                  title='Mean Temp NYC 2016')\n",
    "ax.set_ylabel('Temp (F)')\n",
    "ax.set_xlabel(None);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Text Styling"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Text styling can be applied to any text generating method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.DataFrame({ 'a': [1, 2, 3, 4, 5],\n",
    "                    'b': [1, 3, 6, 8, 10] })\n",
    "\n",
    "ax = df.plot()\n",
    "\n",
    "ax.set_title('Hey!!', size=40, backgroundcolor='grey')\n",
    "\n",
    "ax.set_ylabel('Getting warmer...', rotation=45, color='red')\n",
    "\n",
    "# size=10|smaller|x-large   # font size\n",
    "# position=(x,y)\n",
    "# rotation=degrees|vertical|horizontal\n",
    "# backgroundcolor=name|hex\n",
    "# color=name|hex\n",
    "# family=serif|sans-serif|cursive|fantasy|monospace\n",
    "# style=normal|italic|oblique\n",
    "# weight=normal|bold|heavy|light|ultrabold|ultralight\n",
    "# ha=center|left|right\n",
    "# va=top|bottom|center|baseline|center_baseline\n",
    "# visible=True|False\n",
    "# linespacing=float\n",
    "# fontname='Courier'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "More information can be found here:<br><br>\n",
    "<A HREF=\"https://matplotlib.org/3.1.1/tutorials/text/text_props.html\">text properties tutorial</A><br><br>\n",
    "<A HREF=\"https://matplotlib.org/3.1.1/api/text_api.html#matplotlib.text.Text\">matplotlib Text() object</A>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  Saving the figure, setting the figure size"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  <B>savefig()</B> on the <B>plt</B> module or <B>Figure</B> objects saves the figure to an image file.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The <B>dpi=</B> argument can control the size through 'dots per inch'.\n",
    "\n",
    "The extension of the filename determines what type of image is saved -- see options below.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# when plotting through plt or DataFrame\n",
    "plt.savefig('mychart.png', dpi=400)\n",
    "\n",
    "# when plotting through a Figure object (or Figure from DataFrame:\n",
    "fig.savefig('thischart.jpg', dpi=400);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "print(json.dumps(fig.canvas.get_supported_filetypes(), indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "help(plt.savefig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### bar chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.Series({ 'Toronto': 5.4,\n",
    "                 'Montreal': 3.5,\n",
    "                 'Vancouver': 2.2,\n",
    "                 'Calgary': 1.2    },\n",
    "                name='population')\n",
    "ax = df.plot(kind='bar', rot=0);\n",
    "ax.set_ylabel('pop. (MM)');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### clustered bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([[4,8,5,7,6],[2,3,4,2,6],[4,7,4,7,8],[2,6,4,8,6],[2,4,3,3,2]])\n",
    "df=pd.DataFrame(a, columns=['a','b','c','d','e'], index=[2,4,6,8,10])\n",
    "print(df)\n",
    "ax = df.plot.bar();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  horizontal bar ('barh') chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'lab':['A', 'B', 'C'], 'val':[10, 30, 20]})\n",
    "ax = df.plot.barh(x='lab', y='val')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### plot a DataFrame to horizontal bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "speed = [0.1, 17.5, 40, 48, 52, 69, 88]\n",
    "lifespan = [2, 8, 70, 1.5, 25, 12, 28]\n",
    "index = ['snail', 'pig', 'elephant',\n",
    "         'rabbit', 'giraffe', 'coyote', 'horse']\n",
    "df = pd.DataFrame({'speed': speed,\n",
    "                   'lifespan': lifespan}, index=index)\n",
    "ax = df.plot.barh()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  box plot:  distribution of values in quartiles.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(1234)\n",
    "df = pd.DataFrame(np.random.randn(10,4),\n",
    "                  columns=['Col1', 'Col2', 'Col3', 'Col4'])\n",
    "ax = df.boxplot(column=['Col1', 'Col2', 'Col3'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  histogram"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When we draw a dice 6000 times, we expect to get each value around 1000 times. But when we draw two dices and sum the result, the distribution is going to be quite different. A histogram illustrates those distributions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(\n",
    "np.random.randint(1, 7, 6000),\n",
    "columns = ['one'])\n",
    "df['two'] = df['one'] + np.random.randint(1, 7, 6000)\n",
    "ax = df.plot.hist(bins=12, alpha=0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  pie plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'mass': [0.330, 4.87 , 5.97],\n",
    "                   'radius': [2439.7, 6051.8, 6378.1]},\n",
    "                   index=['Mercury', 'Venus', 'Earth'])\n",
    "ax = df.plot.pie(y='mass', figsize=(5, 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  scatter plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],\n",
    "                   [6.4, 3.2, 1], [5.9, 3.0, 2]],\n",
    "                  columns=['length', 'width', 'species'])\n",
    "ax = df.plot.scatter(x='length',\n",
    "                     y='width',\n",
    "                     c='DarkBlue')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## documentation for further study"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
