{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "594e322a-501a-455e-899a-f0ae8aa801bb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import random\n",
    "from textwrap import wrap\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc63b6b0-7cfc-4b42-919f-4c6d30803f6b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Ergebnisse"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ab1a732-e48a-4ebf-bf36-e61b09dbdac7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Random Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64c7fa25-3576-42e3-96ba-7f94a5a85081",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Fühle die gegebene leere Liste `rands` mit 10 Zufälligen Werten zwischen 1 & 10, mittels der Funktion `random.randint`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "dcdb30d3-31c8-4964-854b-34ecfebfa4ac",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 10, 7, 4, 2, 4, 4, 4, 4, 5]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rands = [random.randint(1,10) for _ in range(10)]\n",
    "rands"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f9a838b-17d0-479c-91aa-f66469bbddab",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "**Aufgabe**: Gegeben sei die Liste `gaussians`; Setzen Sie den Seed des Zufallsgenerator auf den Wert `420` und füllen Sie die Liste `gaussians` mit `20` Elementen mittels der Funktion `random.gauss`. Dabei soll $\\mu = 50$ sein."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e5a3fcd1-c67b-4cda-838d-fe0d6c330f17",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32560 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "random.seed(420)\n",
    "gaussians = [random.gauss(mu=50, sigma=1.0) for _ in range(20)]\n",
    "plt.plot(range(len(gaussians)), gaussians)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2dda921-6f40-4f13-aa8e-b48cef6cbe62",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "Schreibe einen _Linear Congruent Generator_ mit dem funktionsnamen lcg. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "63505203-288f-4cd7-b8ad-d907d1dfdea1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "308.981.078.012.015.624.8\n"
     ]
    }
   ],
   "source": [
    "def lcg(seed: int, scalar: int, modulus: int, offset: int) -> int:\n",
    "    ''' \n",
    "    Linear Congruential Generators\n",
    "\n",
    "    X(n+1) = (a X(n) + c) mod m; n >= 0\n",
    "\n",
    "    m > 0; \n",
    "    0 <= a < m;\n",
    "    c > 0; a > 0\n",
    "\n",
    "    '''\n",
    "    assert modulus > 0, \"Modulus must be greater than 0\"\n",
    "    assert 0 <= scalar and scalar < modulus, \"Scalar must be in range 0 <= a < m\"\n",
    "    assert seed >= 0, \"Seed must be greater than 0\"\n",
    "    return (scalar*seed+offset) % modulus\n",
    "\n",
    "print('.'.join(wrap(str(lcg(3935559000370003845, 3203021881815356449, 2**64-1, 11742185885288659963)), 3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f3e4cf5-454a-4f72-aca6-557c98063fb5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "Nutze die vorher geschriebene `lcg` Funktion, um einen `mcg` korrekt zu implementiert."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "716dd632-ec48-4bc1-b52d-a947f4284bf7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "979.436.896.854.104.790.0\n"
     ]
    }
   ],
   "source": [
    "def mcg(seed: int, scalar: int, modulus: int) -> int:\n",
    "    '''\n",
    "    Multiplicative Congruential Generator\n",
    "    or Lehmer Generator\n",
    "\n",
    "    Just the same as lcg with the Property c=0 (offset = 0)\n",
    "    '''\n",
    "    return lcg(seed, scalar, modulus, 0)\n",
    "\n",
    "print('.'.join(wrap(str(mcg(3935559000370003845, 3203021881815356449, 2**64-1)), 3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ecdd3ce-888d-4727-8f39-bf2d37acc772",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "Definieren Sie eine Variable `sg`, welche eine `SeedSequence` mit dem Startwert `42` hat. Nutzen Sie die verlinkte Dokumentation [Numpy PCG64](https://numpy.org/doc/stable/reference/random/bit_generators/pcg64.html) als Referenz."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "285bb574-ca89-422f-87cc-3faaabd45705",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SeedSequence(\n",
       "    entropy=42,\n",
       ")"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sg = SeedSequence(42)\n",
    "sg"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "368264ca-48e3-4d1e-b6b3-ba39e2c0f8a0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "Nutzen Sie ihre aus letzter Aufgabe erstellte `SeedSequence sg` und erzeugen Sie mittels `Generator` & `PCG64`, 10 Zufallszahlen in der vorgegebenen Liste `pcgs`.\n",
    "Als Referenz schauen Sie gerne in das Beispiel der Dokumentation [Numpy PCG64](https://numpy.org/doc/stable/reference/random/bit_generators/pcg64.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "201a9d6a-d7b2-47d5-b44e-276b484d1dd8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "pcgs = list()\n",
    "sg = SeedSequence(42) # Benötigt keine erneute Definition\n",
    "for s in sg.spawn(10):\n",
    "    pcgs.append(Generator(PCG64(s)).random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "fee352e7-32a6-4388-bfe7-7675822f0bee",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sg = SeedSequence(42) # Benötigt keine erneute Definition\n",
    "pcgs = [\n",
    "    Generator(PCG64(s)).random()\n",
    "    for s in SeedSequence(42).spawn(10)\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "285599e2-9441-4ea1-a911-26bcc30d9123",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33636 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(range(len(pcgs)), pcgs, color='r', label='PCG')\n",
    "plt.title(\"PCG Random Numbers\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a02d109-f840-4ca0-a113-61f5bf0fc9f0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe - Zufallswerte generieren\n",
    "\n",
    "Nutzen Sie ihr Wissen aus vorangegangem Kapitel und füllen Sie die Liste `clients` mit 60 zufälligen Werten aus einem PCG64. Nutzen Sie als Startwert `420` für die `SeedSequence`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "e26997d6-f955-4679-b354-2f4e33e9ec04",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "clients = [\n",
    "    Generator(PCG64(s)).random()\n",
    "    for s in SeedSequence(420).spawn(60)\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "6aa98d07-1ad3-4753-9142-a5238a9ca6cd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "clients = list()\n",
    "sg = SeedSequence(420) # Benötigt keine erneute Definition\n",
    "for s in sg.spawn(60):\n",
    "    clients.append(Generator(PCG64(s)).random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "a5c93b6b-ea90-4524-a64c-6bb28a54769d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 51960 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(range(len(clients)), clients, color='r', label='Clients')\n",
    "plt.title(\"Verkehrsaufkommen an einer Antenne (in % pro minute)\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dcb2740-1f4f-4ca5-bd7c-1d221b033a7f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe - Verkehrswert berechnen\n",
    "\n",
    "Der Verkehrswert berechnet sich mit $$y = \\frac{Y}{t}$$\n",
    "\n",
    "Dabei ist:\n",
    "- $Y$ die Verkehrsmenge\n",
    "- $t$ die Beobachtungsdauer (in minuten)\n",
    "\n",
    "Der Verkehrswert beschreibt die mittlere Nutzung pro Minute.\n",
    "\n",
    "Nutzen Sie die gegebene Formel und die Liste `clients` um den Verkehrswert zu berechnen. Speichern Sie das Ergebnis in der Variablen `y`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "b6b9e73e-cadb-4168-a5ab-9ba4f6578301",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5221268844976396"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = sum(clients)/len(clients)\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "369d9991-41bf-4ffd-8cd9-4d2e04ccc04e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5221268844976394"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.mean(clients)\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68742bfb-4268-4b04-8670-693525773741",
   "metadata": {},
   "source": [
    "Besser Formatiert:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "3f831dc0-dbaa-44b3-8b5d-34e45b93a459",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "52.21%\n"
     ]
    }
   ],
   "source": [
    "print(\"{:.2f}%\".format(y*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "072f5966-b85a-4116-abb4-cdc363dd4bf9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 55052 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(range(len(clients)), clients, color='b', label='Clients')\n",
    "plt.plot(range(len(clients)), [y for _ in range(len(clients))], color='y', label='Verkehrswert')\n",
    "plt.title(\"Verkehrsaufkommen an einer Antenne (in % pro minute)\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16c81e1a-9a5f-44bd-8167-3f56611d4b07",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6202b1d8-6f45-4f35-857e-00a1791c3b33",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "Erstellen Sie ein NumPy Array, welches 6 Nullen reserviert und speichern Sie das Array in der Variablen `only_zeros`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b1eb663f-7528-437c-9aa0-d6fa14ac13b5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "only_zeros = np.zeros(6)\n",
    "only_zeros"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67f4ccf8-cdde-4b17-8a33-ff95c1f7b943",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "Erstellen Sie ein NumPy Array mit 11 Elementen mittels `linspace`, Dabei soll der Startwert = -4 und der Endwert = 17 sein. Speichern Sie das Ergbniss in der Variablen `x_scale`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d6177a1d-5b7f-4a37-9a09-f1d138c11102",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-4. , -1.9,  0.2,  2.3,  4.4,  6.5,  8.6, 10.7, 12.8, 14.9, 17. ])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_scale = np.linspace(-4, 17, num=11)\n",
    "x_scale"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3da779ff-3fc9-4422-b92a-170ae0425700",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "Ihnen ist das Array `pcgs` gegeben dieses enthält zufällige Zahlen eines _Permuted Congruent Generators_. Nutzen Sie Numpy um die Werte des Arrays `pcgs` zu sortieren. Speichern Sie ihr Ergebnis in der Variablen `sorted_pcgs`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "036b178f-43f2-4c36-b40f-2fcee50854e2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[91.67441575549084,\n",
       " 46.74907799518424,\n",
       " 7.123920291270869,\n",
       " 76.39328676507445,\n",
       " 1.7567502091441867,\n",
       " 25.302055214458075,\n",
       " 63.618561250625696,\n",
       " 0.1579146041553514,\n",
       " 70.96566546463475,\n",
       " 29.830322658786066,\n",
       " 32.993271323881935,\n",
       " 85.498191941231,\n",
       " 28.897614421550255,\n",
       " 7.23902480784705,\n",
       " 70.31144257136475,\n",
       " 24.870797377171648,\n",
       " 15.503033920124121,\n",
       " 20.10861125030664,\n",
       " 46.93021735717943,\n",
       " 47.12091752159737]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy.random import SeedSequence, Generator, PCG64\n",
    "sg = SeedSequence(42)\n",
    "pcgs = [Generator(PCG64(s)).random()*100 for s in sg.spawn(20)]\n",
    "pcgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "91623edb-226c-443e-9811-9157e894b6d0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.1579146 ,  1.75675021,  7.12392029,  7.23902481, 15.50303392,\n",
       "       20.10861125, 24.87079738, 25.30205521, 28.89761442, 29.83032266,\n",
       "       32.99327132, 46.749078  , 46.93021736, 47.12091752, 63.61856125,\n",
       "       70.31144257, 70.96566546, 76.39328677, 85.49819194, 91.67441576])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted_pcgs = np.sort(pcgs)\n",
    "sorted_pcgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f7f9b821-7a5b-4210-92d7-6fbaf15f7783",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 49996 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(np.arange(len(sorted_pcgs)), pcgs, color='r', label='PCGs')\n",
    "plt.plot(np.arange(len(sorted_pcgs)), sorted_pcgs, color='g', label='Sortierte PCGs')\n",
    "plt.title(\"PCG Random Numbers\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddfd582e-7d17-42d4-a2d3-54573f174736",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "Schauen Sie in die NumPy Dokumentation zur Funktion [reshape](https://numpy.org/doc/stable/reference/generated/numpy.reshape.html#numpy.reshape). Und shapen Sie das Array `n3` in eine Matrix der Form 3x2. Speicher Sie ihr Ergebnis in der Variablen `m23`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9ddfbdcc-2bff-41bd-9027-a76f789e0f8a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[21, 22],\n",
       "       [23, 24],\n",
       "       [25, 26]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n3 = np.arange(21, 27) # Aus dem Beispiel\n",
    "m23 = n3.reshape((3, 2))\n",
    "m23"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ac32d1b-9b1c-4bb8-a108-d54f40b12f79",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Matplotlib"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d98f1fb2-d5ea-42a1-81a4-759cf0656bec",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe - Erster eigener Plot Square Root\n",
    "\n",
    "Analog zu voheriger Erklärung Plotten Sie im folgenden die Funktion Square Root Mathematisch definiert als $f(x) = \\sqrt x; \\quad x \\geq 0$.\n",
    "\n",
    "Gehen Sie dabei wie folgt vor:\n",
    "1. Definieren Sie einen geeigneten [Linespace](https://numpy.org/doc/stable/reference/generated/numpy.linspace.html#numpy-linspace) für die Zahlenraum 0...100. (Tipp: Achten Sie auf die Definition! Die Wurzel ist nur für positive Zahlen definiert.)\n",
    "2. Berechnen Sie mittels der Funktion [np.sqrt](https://numpy.org/doc/stable/reference/generated/numpy.sqrt.html#numpy.sqrt) die Werte für die Wurzel.\n",
    "3. Plotten Sie das Ergebnis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c9c0cd10-cd1c-4555-9c6e-df0c69d011f6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19556 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "xs = np.linspace(0, 100, num=200)\n",
    "ys = np.sqrt(xs)\n",
    "plt.plot(xs, ys)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "145c3da1-bbe6-4987-86ec-ea31da39cd66",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19628 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "xs = np.linspace(0, 100, num=50)\n",
    "ys = np.sqrt(xs)\n",
    "plt.plot(xs, ys)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e3489dd-f4f6-4217-ac4f-4781aa5cb8a4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe - Multiplot\n",
    "\n",
    "In der nächsten Aufgabe wollen wir gleich zwei Funktionen Plotten. $f(x) = \\sqrt x$ und $g(x) = x^2$.\n",
    "\n",
    "Gehen Sie dabei wie folgt vor:\n",
    "1. Definieren Sie einen geeigneten [Linespace](https://numpy.org/doc/stable/reference/generated/numpy.linspace.html#numpy-linspace) für die Zahlenraum 0...3. (Tipp: Achten Sie auf die Definition! Die Wurzel ist nur für positive Zahlen definiert.)\n",
    "2. Berechnen Sie mittels der Funktion [np.sqrt](https://numpy.org/doc/stable/reference/generated/numpy.sqrt.html#numpy.sqrt) die Werte für die Wurzel.\n",
    "3. Berechnen Sie mittels der Funktion [np.square](https://numpy.org/doc/stable/reference/generated/numpy.square.html#numpy-square) die Werte für die Quadrat Zahlen\n",
    "4. Geben Sie den beiden Plots die Farben Grün & Rot. Nutzen Sie gerne die [Color Shorthands](https://matplotlib.org/stable/users/explain/colors/colors.html) aus der Dokumentation.\n",
    "5. Plotten Sie die Square Funktion mit dem Linestyle `dashdot`, wie der Dokumentation zu entnehmen [Linestyles](https://matplotlib.org/stable/gallery/lines_bars_and_markers/linestyles.html)\n",
    "6. Geben Sie den beiden Plots angemessene Labels.\n",
    "7. Fügen Sie die Legende hinzu.\n",
    "8. Plotten Sie das Ergebnis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0d273b1b-f941-4dac-aee9-588201289058",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26372 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "xt = np.linspace(0, 3, num=200)\n",
    "sqrt = np.sqrt(xt)\n",
    "square = np.square(xt)\n",
    "\n",
    "plt.plot(xt, sqrt, color='r', label=\"Square Root\")\n",
    "plt.plot(xt, square, color='g', label=\"Square Function\", linestyle=\"dashdot\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13234690-1e92-4fc1-ad61-7ddd609b926b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "### Aufgabe\n",
    "\n",
    "Ihnen ist ein Datenset `sec_school` einer Hauptschule gegeben, welches die Klassenstufen von 5 bis 9 auf die Anzahl ihrer Schüler im Jahrgang mappt. \n",
    "\n",
    "Definieren Sie einen Barplot. Gehen Sie dabei wie folgt vor:\n",
    "1. Definieren Sie ein geeignetes Farbschema zur Darstellung der Daten.\n",
    "2. Extrahieren Sie die Schlüssel und Werte aus dem Datenset und übergeben Sie diese zusammen mit den Farbwerten an die Funktion `plt.bar`.\n",
    "3. Setzen Sie geeignete Werte für die X & Y-Achse.\n",
    "4. Setzen Sie einen geeigneten Titel für den Plot.\n",
    "5. Plotten Sie den Werte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "00659931-1405-4a59-a162-b594e29c47ff",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sec_school = {\n",
    "    '5. Klasse': 29,\n",
    "    '6. Klasse': 35,\n",
    "    '7. Klasse': 25,\n",
    "    '8. Klasse': 28,\n",
    "    '9. Klasse': 31\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "84da3c47-5ef5-4157-bb69-7ec972a86957",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26476 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "colors = [\"red\", \"blue\", \"green\", \"yellow\", \"pink\"]\n",
    "plt.bar(sec_school.keys(), sec_school.values(), color=colors)\n",
    "plt.xlabel(\"Klassenstufe\")\n",
    "plt.ylabel(\"Anzahl Kinder\")\n",
    "plt.title(\"Verteilung Kinder einer Hauptschule pro Klassenstufe\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d42a1793-d23d-459e-9f8f-d56f9c4ba0a8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "# Student Answers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "834e94b0-dbf4-473b-ba35-1acc09aac894",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17632 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bar_colors = [\"lavender\", \"#C79FEF\", \"orchid\", \"plum\", \"purple\"]\n",
    "\n",
    "plt.bar(sec_school.keys(), sec_school.values(), color=bar_colors)\n",
    "\n",
    "plt.ylabel(\"Anzahl Kinder\") \n",
    "plt.xlabel(\"Klassenstufen\") \n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "ac88b1db-9bf6-458e-8fbf-02184e20cb66",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24732 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "colors = ['blue', 'green', 'orange', 'red', 'purple'] # Aufgabe 1\n",
    "# Aufgabe 2\n",
    "plt.bar(list(sec_school.keys()), list(sec_school.values()), color=colors)\n",
    "# Aufgabe 3, 4\n",
    "plt.xlabel('Klassenstufen')\n",
    "plt.ylabel('Anzahl der Schüler')\n",
    "plt.title('Anzahl der Schüler pro Klasse')\n",
    "plt.show() # Aufgabe 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "4077a6ef-284f-4d7a-a54e-f6343da61003",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 21948 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bar_farbe = ['b','g','m','y','c']\n",
    "plt.bar(sec_school.keys(), sec_school.values(), color=bar_farbe)\n",
    "plt.ylabel(\"Anzahl der Kinder\")\n",
    "plt.xlabel(\"Klassenstufen\")\n",
    "plt.title(\"Hauptschulklassen\")\n",
    "plt.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "233d9caf-f1df-454b-abe2-40ae0cdac2d6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 25808 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "colors = ['skyblue', 'lightgreen', 'lightcoral', 'lightsalmon', 'lightseagreen']\n",
    "# Barplot erstellen # Schlüssel (Klassenstufen) und Werte (Anzahl der Schüler) extrahieren\n",
    "plt.bar(list(sec_school.keys()), list(sec_school.values()), color=colors)\n",
    "# Achsentitel setzen\n",
    "plt.xlabel('Klassenstufen')\n",
    "plt.ylabel('Anzahl der Schüler')\n",
    "plt.title('Anzahl der Schüler pro Klassenstufe')\n",
    "# Plot anzeigen\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "024fdbcd-b040-40f9-94ea-79022fdfa7c6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27032 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.title(\"Sekundarstufe Sonne&Beton Hauptschule\")\n",
    "#color ist optionaler Parameter/Attribut der Funktion plt.bar definiert als leere Liste\n",
    "bar_colors = [\"lightgrey\", \"yellow\", \"silver\",\"darkgrey\" ,\"gold\"]\n",
    "#übergeben Sie diese zusammen mit den Farbwerten an die Funktion plt.bar\n",
    "plt.bar(sec_school.keys(), sec_school.values(), color=bar_colors, edgecolor=[\"k\"]) #Parameter color werden die Farben aus bar_colors übergeben \n",
    "plt.ylabel(\"Anzahl der Kinder\") # Beschriften der Y-Achse\n",
    "plt.xlabel(\"Klassenstufe\") # Beschriften der X-Achse\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1c5288f-a081-4dd8-8514-99c322633759",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Aufgabe\n",
    "\n",
    "Ihnen ist ein Datenset `sec_school` einer Hauptschule gegeben, welches die Klassenstufen von 5 bis 9 auf die Anzahl ihrer Schüler im Jahrgang mappt. \n",
    "\n",
    "Definieren Sie einen Pieplot. Gehen Sie dabei wie folgt vor:\n",
    "1. Definieren Sie ein geeignetes Farbschema zur Darstellung der Daten.\n",
    "2. Extrahieren Sie die Schlüssel und Werte aus dem Datenset und übergeben Sie diese zusammen mit den Farbwerten an die Funktion `plt.pie`. (Nutzen Sie zum Anzeigen der Prozentwerte autopct='%1.1f%%')\n",
    "3. Lassen Sie die 6. Klasse 25% und die 9. Klasse 40% explodieren.\n",
    "4. Setzen Sie einen geeigneten Titel für den Plot.\n",
    "5. Plotten Sie den Werte."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "599e8022-ad23-47a9-a24d-73cbf62e06ba",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 41876 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.pie(\n",
    "    sec_school.values(),\n",
    "    labels=sec_school.keys(),\n",
    "    autopct='%1.1f%%',\n",
    "    explode=[0, 0.25, 0, 0, 0.4]\n",
    ")\n",
    "\n",
    "plt.title(\"Klassenverteilung einer Grundschule\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d43dd17-1643-4eea-8370-0ec9f33af261",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "# Student Answers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "318ce080-7439-4483-8127-4adfa2f8bea4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 41672 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sec_color = ['#93afdb','#92d1d0','#ad92d1','#d192c0','#d1b692']\n",
    "klassen = list(sec_school.keys())\n",
    "schülerzahl = list(sec_school.values())\n",
    "explode = (0, 0.25, 0, 0, 0.4)\n",
    "plt.title(\"Schülerverteilung der Hauptschule\")\n",
    "plt.pie(schülerzahl, labels=klassen, colors=sec_color, autopct='%1.1f%%', explode=explode)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "09759e27-3e84-4923-97e7-483bb6c38dbb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 44312 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pie_colors = [\"lightblue\", \"lightgreen\", \"lightcoral\", \"lightskyblue\", \"lightpink\"]\n",
    "\n",
    "explode = (0, 0.1, 0, 0, 0.2)\n",
    "\n",
    "plt.pie(sec_school.values(), labels=sec_school.keys(), colors=pie_colors, autopct='%1.1f%%', explode=explode)\n",
    "plt.title(\"Anzahl der Schüler in verschiedenen Klassenstufen\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "047145a2-e3be-44ba-a1a6-2e3b206c07c6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 54544 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "schluessel = list(sec_school.keys())\n",
    "werte = list(sec_school.values())\n",
    "farben = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'purple']\n",
    "explode = (0, 0.25, 0, 0, 0.40)\n",
    "\n",
    "plt.pie(werte, explode=explode, labels=schluessel, colors=farben, autopct='%1.1f%%', shadow=True, startangle=140)\n",
    "plt.title('Verteilung der Klassenstärke')\n",
    "plt.axis('equal')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "24e56d44-d05b-4be8-a57b-84b7c6f4f5a3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 40232 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "color_pie=(\"gold\", \"yellow\", \"gray\", \"silver\", \"lightgrey\")\n",
    "plt.pie(sec_school.values(), labels=sec_school.keys(), colors=color_pie, autopct='%1.1f%%', explode=[0, 0.25, 0, 0, 0.40])\n",
    "\n",
    "plt.title(\"Klassenverteilung Sonne&Beton Hauptschule\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "122ccc8b-03d3-42b2-b22a-948b91bdebd5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": []
   },
   "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
