{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.1: Distributions and histograms\n",
    "\n",
    "Look up another distribution available from numpy random. Generate some data points, fill them into a histogram and show it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We will use a poisson generator here.\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# We can store functions in variables as well:\n",
    "rand = np.random.poisson\n",
    "\n",
    "data = rand(5, 10000) # Mu of the distribution is 5 and we\n",
    "# generate 10000 values\n",
    "\n",
    "# Showing the resulting distribution:\n",
    "plt.hist(data)\n",
    "plt.show()\n",
    "\n",
    "\n",
    "### Additional advice:\n",
    "# You can set the number of bins and the range of the \n",
    "# histogram to greatly improve your histograms:\n",
    "plt.hist(data, bins=21, range=[-0.5, 20.5])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2: Enhance your histogram\n",
    "Add a label, x- and y- axis labels and a legend to your histogram. Use a different color and save it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(6, 4))\n",
    "plt.hist(data, color='orange', bins=21, range=[-0.5, 20.5], label='Poisson distribution')\n",
    "plt.xlabel('x', fontsize=12)\n",
    "plt.ylabel('counts per bin', fontsize=12)\n",
    "plt.legend(loc='upper right', fontsize=12)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.savefig('plot_different_colour.png')\n",
    "\n",
    "plt.show()\n",
    "\n",
    "#### There is also a way to modify the font size of the ticks (of course there is ...).\n",
    "#### You should feel free to look up on google how to use rcParams and axis objects."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3: Red dotted sine function\n",
    "Create an array from 0 to 2pi (np.pi is available ...) and compute the sine function of it. Plot it to a canvas using a red dashed line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##### Basic solution\n",
    "\n",
    "# We need to import the modules numpy as matplotlib.pyplot:\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "#### Step by step solution\n",
    "\n",
    "# Create equally distributed numbers between 0 and 2*pi:\n",
    "x = np.linspace(0, 2*np.pi)\n",
    "\n",
    "# Get a figure of nice size:\n",
    "plt.figure(figsize=(6, 4))\n",
    "# Plot the function and set the style to 'r--' -> 'r'=red, '--' is the dashed line option:\n",
    "plt.plot(x, np.sin(x), 'r-.')\n",
    "\n",
    "# Display the figure.\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##### More detailed solution with additional information\n",
    "\n",
    "# The modules stay imported after executing the cell above.\n",
    "# On restart you would have to import them again before this cell works.\n",
    "\n",
    "# Get a figure of nice size:\n",
    "plt.figure(figsize=(6, 4))\n",
    "\n",
    "# Plot the function and set the style to 'r--' -> 'r'=red, '--' is the dashed line option:\n",
    "plt.plot(x, np.sin(x), 'r--',\n",
    "         # if you want to use latex backslashes in text, you need to put an 'r' in front of the string\n",
    "         label=r'$f(x) = \\sin(x)$')\n",
    "\n",
    "# Set up axis labels and legend:\n",
    "plt.xlabel('$x$')\n",
    "plt.ylabel('$f(x)$')\n",
    "\n",
    "# If you like to, turn on the grid.\n",
    "plt.grid(True) # True is optional, without argument it will alternate between switching on and off\n",
    "\n",
    "# If you like to, you can set a limit on the x axis, ylim works equally for the other axis.\n",
    "plt.xlim(0, 2*np.pi)\n",
    "\n",
    "# Let's put the legend up right:\n",
    "plt.legend(loc='upper right')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.4: Slicing plots\n",
    "Plot positive values black, negative ones red."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The modules are still imported from the cells above!\n",
    "\n",
    "plt.figure(figsize=(6,4))\n",
    "\n",
    "# We could keep using the x from before as well but it looks cleaner like this.\n",
    "x = np.linspace(0, 2*np.pi)\n",
    "\n",
    "# Compute the f(x) values.\n",
    "y = np.sin(x)\n",
    "\n",
    "# Using the y[y>0] and y[y<0] slicing technique and we also use a new marker:\n",
    "plt.plot(x[y>=0], y[y>=0], 'ko', label='positive')\n",
    "plt.plot(x[y<0], y[y<0], 'ro', label='negative')\n",
    "\n",
    "# Add the grid.\n",
    "plt.grid(True)\n",
    "\n",
    "# Put the legend in somewhere.\n",
    "# You can also change fonts and everything else pretty much, just have a look on google!\n",
    "plt.legend(loc='upper right', fontsize=14)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.5: Add gaussian noise\n",
    "Add gaussian noise to the sine plot and use the same color for all data points again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The modules are still imported from the cells above!\n",
    "plt.figure(figsize=(6, 4))\n",
    "\n",
    "# Let's take some more values than only 50.\n",
    "x = np.linspace(0, 2*np.pi, 200)\n",
    "\n",
    "# Compute the f(x) values.\n",
    "# The np.random.normal() takes the mu values (means) as first argument, the second is sigma.\n",
    "y = np.random.normal(np.sin(x), 0.3)\n",
    "\n",
    "# Plot the function and take small dots as markers.\n",
    "plt.plot(x, y, '.', label='noisy sine function')\n",
    "\n",
    "# Put the legend in somewhere.\n",
    "# You can also change fonts and everything else pretty much, just have a look on google!\n",
    "plt.legend(loc='upper right', fontsize=14)\n",
    "\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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
