{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting in Python\n", "\"[`Matplotlib`](https://matplotlib.org/) is a comprehensive library for creating static, animated, and interactive visualizations in Python.\" There are several [tutorials](https://matplotlib.org/tutorials/index.html), where you can learn the usage. \"`matplotlib.pyplot` is a collection of functions that make matplotlib work like MATLAB. Each `pyplot` function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list1 = range(5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(list1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list2 = [random.random() for _ in range(5)]\n", "print(list2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(list1, list2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(list1, list2, 'gs') # try: 'r^' 'gs' 'k--' 'b:' 'ro-'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.scatter(list1, list2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.bar(list1, list2, .7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = [i*0.01 for i in range(251)]\n", "t2 = [(i*0.01)**2 for i in range(251)]\n", "t3 = [(i*0.01)**3 for i in range(251)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(t, t2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(t, t2, 'r', t, t3, 'g')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(12, 3))\n", "\n", "plt.subplot(131)\n", "plt.bar(list1,list2)\n", "\n", "plt.subplot(132)\n", "plt.scatter(list1,list2)\n", "\n", "plt.subplot(133)\n", "plt.plot(list1,list2)\n", "\n", "plt.suptitle('Functions')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(8, 12))\n", "\n", "plt.subplot(311)\n", "plt.bar(list1,list2)\n", "plt.title(\"Bar\")\n", "\n", "plt.subplot(312)\n", "plt.scatter(list1,list2)\n", "plt.title(\"Scattered\")\n", "\n", "plt.subplot(313)\n", "plt.plot(list1,list2)\n", "plt.title(\"Plot\")\n", "\n", "plt.suptitle('Functions')\n", "\n", "plt.tight_layout() \n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***Exercise:*** Copy the next code into a file named `4test.py` and run it from a terminal with the command `python3 4test.py 4` \n", "\n", "\n", "```Python\n", "import matplotlib.pyplot as plt\n", "import random\n", "import sys\n", "\n", "def my_plot():\n", " \"\"\"\n", " Plotting n random values.\n", " n is read in from the terminal.\n", " \"\"\"\n", " \n", " n = int(sys.argv[1])\n", " \n", " list1 = range(n)\n", " list2 = [random.random() for _ in range(n)]\n", " list3 = [i**2 for i in list2]\n", " \n", " plt.subplot(311)\n", " plt.bar(list1,list1)\n", " plt.subplot(312)\n", " plt.bar(list1,list2)\n", " plt.subplot(313)\n", " plt.bar(list1,list3)\n", " plt.show()\n", "\n", "def main():\n", " my_plot()\n", "\n", "if __name__ == \"__main__\":\n", " main()\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For calculating factorial, binomial coefficient, exponential function you may use the `math` module: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the `math` module\n", "\n", "To compute the factorial or the exponential function you can use the `math` module." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math\n", "\n", "math.factorial(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a `comb` function to calculate the binomial coefficient (from Python 3.8 there also exists a function `math.comb()` for this):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def comb(n, m):\n", " return math.factorial(n)//math.factorial(m)//math.factorial(n-m)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "comb(25, 10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "math.exp(1), math.exp(2) # the e^x function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***Exercise:*** Throw a die 10 times. What is the probability that you get 6 exactly 2 times. Calculate with the formula for the binomial distribution (the parameters will be $n=10$, $p=1/6$), and approximate it with Poisson-distribution, with the parameter $\\lambda$, where $\\lambda=n\\cdot p$ is the expected value of the binomial distribution." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 10\n", "p = 1/6\n", "m = 2\n", "l = n*p # lambda\n", "\n", "binomial_dist = comb(n, m) * p**m * (1-p)**(n-m)\n", "poison_dist = l**m * math.exp(-l) / math.factorial(m)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "binomial_dist, poison_dist" ] }, { "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.12.3" } }, "nbformat": 4, "nbformat_minor": 4 }