{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"\n",
    "    border: 2px solid #4CAF50; \n",
    "    padding: 15px; \n",
    "    background-color: #f4f4f4; \n",
    "    border-radius: 10px; \n",
    "    align-items: center;\">\n",
    "\n",
    "<h1 style=\"margin: 0; color: #4CAF50;\">Visualisierungen in Python 2: Visualisierungstypen</h1>\n",
    "<h2 style=\"margin: 5px 0; color: #555;\">DSAI</h2>\n",
    "<h3 style=\"margin: 5px 0; color: #555;\">Jakob Eggl</h3>\n",
    "\n",
    "<div style=\"flex-shrink: 0;\">\n",
    "    <img src=\"https://www.htl-grieskirchen.at/wp/wp-content/uploads/2022/11/logo_bildschirm-1024x503.png\" alt=\"Logo\" style=\"width: 250px; height: auto;\"/>\n",
    "</div>\n",
    "<p1> © 2024/25 Jakob Eggl. Nutzung oder Verbreitung nur mit ausdrücklicher Genehmigung des Autors.</p1>\n",
    "</div>\n",
    "<div style=\"flex: 1;\">\n",
    "</div>   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Colors and Colormaps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Farben"
   ]
  },
  {
   "attachments": {
    "hue-saturation-value.webp": {
     "image/webp": "[encoded data, 19744 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![hue-saturation-value.webp](attachment:hue-saturation-value.webp)\n",
    "\n",
    "(from https://www.virtualartacademy.com/three-components-of-color/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Farbton\n",
    "\n",
    "* Ist der *Basiston* der Farbe (zBsp.: Gelb, Rot, Blau, Grün, Violett, etc.)\n",
    "* Wird **nur** durch das RGB Verhältnis bestimmt\n",
    "* Farbton bleibt gleich, auch wenn sich die Intensität oder die Helligkeit ändert "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sättigung\n",
    "\n",
    "* Gibt an, wie *grau* eine Farbe ist\n",
    "* Je weiter entfernt eine Farbe von der Gleichverteilung ist, desto gesättigter ist sie\n",
    "* Eine maximal gesättigte Farbe ist zum Beispiel Rot (255, 0, 0), eine niedrig gesättigte Farbe ist (100, 100, 100)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Helligkeit\n",
    "\n",
    "* Gibt an, wie hell/dunkel die Farbe ist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Basierend auf https://manifold-net.translate.goog/doc/mfd8/colors_as_hue_saturation_and_brightness.htm?_x_tr_sl=en&_x_tr_tl=de&_x_tr_hl=de&_x_tr_pto=rq können wir auch folgende Formeln für die Sättigung und Helligkeit verwenden. Dabei verwenden wir $M=\\max\\{R,G,B\\}$ und $m=\\min\\{R,G,B\\}$.\n",
    "\n",
    "$$L = \\frac{M+m}{2}$$\n",
    "\n",
    "$$S = \\begin{cases}\n",
    "    0& R=G=B\\\\\n",
    "    255 \\cdot \\frac{M-m}{M+m}& L<128\\\\\n",
    "    255 \\cdot \\frac{M-m}{511 - (M+m)}\n",
    "\\end{cases}$$\n",
    "\n",
    "wobei $L$ die Helligkeit und $S$ die Sättigung beschreibt."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Colormaps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Es gibt mehrere Arten von Colormaps:\n",
    "    * Sequential\n",
    "    * Diverging\n",
    "    * Categorical"
   ]
  },
  {
   "attachments": {
    "1_11SBac2NeUNmVLrl8fY_vQ.webp": {
     "image/webp": "[encoded data, 24776 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![1_11SBac2NeUNmVLrl8fY_vQ.webp](attachment:1_11SBac2NeUNmVLrl8fY_vQ.webp)\n",
    "\n",
    "(from https://medium.com/@oksana.zamyslova/data-visualisation-3d430868bfa5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sequential Scale\n",
    "\n",
    "* **Einsatz**: Darstellung quantitativer Zusammenhänge\n",
    "    * **Beispiele:** Epidemie- oder Pandemiezahlen (Corona) bzw. Heatmaps, Zeitreihen (Durschnittstemperatur), Wahlergebnisse in einzelnen Gemeinden\n",
    "* **Farbverlauf**: Farben wechseln von dunkel auf hell (oder umgekehrt) bzw. von geringer Intensität zu hoher Intensität (oder umgekehrt)\n",
    "* **Vorteile**:\n",
    "    * Einfache Interpretation\n",
    "* **Nachteile**:\n",
    "    * Barrierefreiheit: Schwer für blinde Menschen\n",
    "    * Begrenzte Differenzierung: Feine Unterschiede können schwer/nicht encodiert werden\n",
    "    * Verzerrung: Zu helle oder dunkle Bereiche könnten visuell überbetont werden, selbst wenn die Werte nur leicht variieren."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Diverging Scale\n",
    "\n",
    "* **Einsatz**: Visualisierung von Daten, die sich um einen zentralen, neutralen Wert gruppieren, i.e. Abweichungen von einem Referenzwert.\n",
    "    * **Beispiele**: Temperaturabweichungen von einem Mittelwert, Finanzdaten (Gewinn/Verlust), politische Umfragen (z.B. Zustimmung/Ablehnung), Umweltdaten (z.B. pH-Werte)\n",
    "* **Farbverlauf**: Zwei Farbtöne bewegen sich symmetrisch von einem neutralen Mittelpunkt in gegensätzliche Richtungen (z.B. Rot für negative und Blau für positive Werte), wobei die Intensität zunimmt.\n",
    "* **Zentraler Referenzpunkt**: Die Skala betont eine Mitte (z.B. Nullpunkt oder Durchschnitt), von der positive und negative Werte abweichen.\n",
    "* **Vorteile**:\n",
    "    * Unterschiede von einem neutralen Punkt sind sofort erkennbar.\n",
    "    * Neutralität: Positiv und negativ werden gleichwertig hervorgehoben, was eine neutrale Präsentation unterstützt.\n",
    "* **Nachteile**:\n",
    "    * Farbverwirrung: Ein unklarer zentraler Punkt oder Schlecht gewählte Farben kann zu Missverständnissen führen.\n",
    "    * Barrierefreiheit: Bestimmte Farbenpaare (z.B. Rot-Grün) sind für farbenblinde Menschen schwer zu unterscheiden.\n",
    "    * Begrenzte Differenzierung: Feine Unterschiede können schwer/nicht encodiert werden\n",
    "* **Best Practices**:\n",
    "    * Der zentrale Wert soll neutral sein, zBsp. weiß oder grau.\n",
    "    * Richtige Farbwahl: Besser Rot-Blau als Rot-Grün (Farbblindheit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Categorical Scale\n",
    "\n",
    "* **Einsatz**: Darstellung von Daten, die unabhängige Kategorien oder Gruppen repräsentieren, bei denen keine natürliche Reihenfolge besteht.\n",
    "    * **Beispiele**: Umfragen mit mehreren Antwortmöglichkeiten\n",
    "* **Farbverlauf**: Jede Kategorie wird durch eine klar voneinander abgegrenzte Farbe dargestellt. Die Farben sollten sich maximal voneinander unterscheiden.\n",
    "* **Vorteile**:\n",
    "    * Eindeutige Unterscheidung: Kontrastreicher Farben ermöglicht eine klare visuelle Trennung der Kategorien.\n",
    "    * Ideal für qualitative Daten: Funktioniert gut, wenn es keine logische Reihenfolge oder Skala gibt.\n",
    "    * Flexibel: Kann in Diagrammen, Karten, Balkendiagrammen und mehr verwendet werden, wo es wichtig ist, jede Kategorie deutlich zu erkennen.\n",
    "* **Nachteile**:\n",
    "    * Begrenzte Anzahl an Farben: Bei zu vielen Kategorien kann es schwierig werden, ausreichend unterscheidbare Farben zu finden, ohne dass die Farben sich zu ähnlich werden.\n",
    "    * Barrierefreiheit: Farbpaletten müssen sorgfältig gewählt werden, da bestimmte Farben (z.B. Rot-Grün) für Menschen mit Farbsehschwäche schwer zu unterscheiden sind.\n",
    "    * Keine Abbildung von Intensität: Im Gegensatz zu sequenziellen oder divergenten Skalen kann eine kategoriale Skala keine quantitativen Unterschiede darstellen.\n",
    "* **Best Practices**:\n",
    "    * Begrenzte Kategorien: Ideal für Datensätze mit einer moderaten Anzahl von Kategorien (in der Regel weniger als 10). Für größere Mengen sollten alternative Methoden (z.B. Texturen oder Symbole) in Kombination mit Farben eingesetzt werden.\n",
    "    * Farbkontraste: Gut unterscheidbare Farben sollen verwendet werden. Tools wie [Colorbrewer](https://colorbrewer2.org/) können helfen\n",
    "    * Konsistenz: Alle Visualisierungen sollten die gleichen Farben verwendet werden um Verwirrung in der Zuordnung zu vermeiden.\n",
    "    * Unterstützende Labels: Farben alleine möglicherweise nicht ausreichend. Legenden und Labels können helfen."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "^# Visualisierungen\n",
    "\n",
    "In diesem Notebook beginnen wir, verschiedene Visualisierungsmöglichkeiten zu besprechen. Dabei werden wir die Vor- und Nachteile, basierend auf den gegebenen Daten, thematisieren."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Balkendiagramm"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 20340 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ergebnisse Nationalratswahl 2019\n",
    "\n",
    "![image.png](attachment:image.png)\n",
    "\n",
    "(from https://de.statista.com/statistik/daten/studie/761326/umfrage/ergebnis-der-nationalratswahl-in-oesterreich/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Eigenschaften eines Balkendiagramms**:\n",
    "* Länge der Balken direkt proportional zu Wert in den Daten\n",
    "* Sowohl relative Häufigkeiten als auch absolute Häufigkeiten möglich\n",
    "* Balken können auch horizontal angeordnet sein\n",
    "* Zusätzlich können Farben benutzt werden (oft nicht notwendig), dabei wird eine *Categorical* Color-Map verwendet.\n",
    "* Weitere Beschriftungen:\n",
    "    * Titel\n",
    "    * Achsenbeschriftung (x und y)\n",
    "    * Legende\n",
    "    * Direkte Platzierung der Werte bei den Balken\n",
    "* Mehrere Features pro Label auch möglich (zBsp.: Stimmenanteil im Vorjahr)"
   ]
  },
  {
   "attachments": {
    "AGD0009-20191003-768x716.jpg": {
     "image/jpeg": "[encoded data, 64340 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![AGD0009-20191003-768x716.jpg](attachment:AGD0009-20191003-768x716.jpg)\n",
    "\n",
    "(von https://www.vienna.at/nationalratswahl-2019-endgueltiges-ergebnis-mit-wahlkarten/6377630)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Nun betrachten wir die Erstellung von (einfachen) Balkendiagrammen in Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import sklearn.linear_model as lm\n",
    "from scipy.stats import pearsonr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Noten\n",
    "grades = {\n",
    "    'SehrGut':6,\n",
    "    'Gut':3,\n",
    "    'Befriedigend':6,\n",
    "    'Genügend':3,\n",
    "    'NichtGenügend':3}\n",
    "\n",
    "df_grades = pd.DataFrame(grades.items(), columns=['Grade', 'Value'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30260 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Balkendiagramm\n",
    "sns.barplot(x='Grade', y='Value', data=df_grades, palette='viridis', hue='Grade', legend='full', orient='v') # viele weitere Möglichkeiten (palette, legend, orientation, etc.)\n",
    "plt.title('Notenverteilung')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Nun mit mehreren Features (Vergleiche 3D Plot von letzter Woche)\n",
    "\n",
    "data = {\n",
    "    'Person': ['Alice', 'Bob', 'Charlie', 'Dora', 'Emil', 'Felix'],\n",
    "    'Bsp1':[3,4,5,2,6,4],\n",
    "    'Bsp2':[5,4,3,2,1,3],\n",
    "    'Bsp3':[1,2,3,4,5,1],\n",
    "    'Bsp4':[6,5,4,3,2,3],\n",
    "    'Bsp5':[2,3,4,5,6,3],\n",
    "    'Bsp6':[4,5,6,1,2,2]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "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>Person</th>\n",
       "      <th>Bsp1</th>\n",
       "      <th>Bsp2</th>\n",
       "      <th>Bsp3</th>\n",
       "      <th>Bsp4</th>\n",
       "      <th>Bsp5</th>\n",
       "      <th>Bsp6</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alice</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Bob</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Dora</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Emil</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Felix</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Person  Bsp1  Bsp2  Bsp3  Bsp4  Bsp5  Bsp6\n",
       "0    Alice     3     5     1     6     2     4\n",
       "1      Bob     4     4     2     5     3     5\n",
       "2  Charlie     5     3     3     4     4     6\n",
       "3     Dora     2     2     4     3     5     1\n",
       "4     Emil     6     1     5     2     6     2\n",
       "5    Felix     4     3     1     3     3     2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26420 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Balkendiagramm mit mehreren Features\n",
    "\n",
    "df_melted = df.melt(id_vars='Person', var_name='Category', value_name='Points')\n",
    "\n",
    "# Plotting with Seaborn\n",
    "plt.figure(figsize=(10, 6))\n",
    "sns.barplot(x='Person', y='Points', hue='Category', data=df_melted, legend='full')\n",
    "\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "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>Person</th>\n",
       "      <th>Category</th>\n",
       "      <th>Points</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alice</td>\n",
       "      <td>Bsp1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Bob</td>\n",
       "      <td>Bsp1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>Bsp1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Dora</td>\n",
       "      <td>Bsp1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Emil</td>\n",
       "      <td>Bsp1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Felix</td>\n",
       "      <td>Bsp1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Alice</td>\n",
       "      <td>Bsp2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Bob</td>\n",
       "      <td>Bsp2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>Bsp2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Dora</td>\n",
       "      <td>Bsp2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Emil</td>\n",
       "      <td>Bsp2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Felix</td>\n",
       "      <td>Bsp2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Alice</td>\n",
       "      <td>Bsp3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Bob</td>\n",
       "      <td>Bsp3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>Bsp3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Dora</td>\n",
       "      <td>Bsp3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>Emil</td>\n",
       "      <td>Bsp3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Felix</td>\n",
       "      <td>Bsp3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>Alice</td>\n",
       "      <td>Bsp4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Bob</td>\n",
       "      <td>Bsp4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>Bsp4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>Dora</td>\n",
       "      <td>Bsp4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>Emil</td>\n",
       "      <td>Bsp4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>Felix</td>\n",
       "      <td>Bsp4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>Alice</td>\n",
       "      <td>Bsp5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>Bob</td>\n",
       "      <td>Bsp5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>Bsp5</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>Dora</td>\n",
       "      <td>Bsp5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>Emil</td>\n",
       "      <td>Bsp5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>Felix</td>\n",
       "      <td>Bsp5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>Alice</td>\n",
       "      <td>Bsp6</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>Bob</td>\n",
       "      <td>Bsp6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>Bsp6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>Dora</td>\n",
       "      <td>Bsp6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>Emil</td>\n",
       "      <td>Bsp6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>Felix</td>\n",
       "      <td>Bsp6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Person Category  Points\n",
       "0     Alice     Bsp1       3\n",
       "1       Bob     Bsp1       4\n",
       "2   Charlie     Bsp1       5\n",
       "3      Dora     Bsp1       2\n",
       "4      Emil     Bsp1       6\n",
       "5     Felix     Bsp1       4\n",
       "6     Alice     Bsp2       5\n",
       "7       Bob     Bsp2       4\n",
       "8   Charlie     Bsp2       3\n",
       "9      Dora     Bsp2       2\n",
       "10     Emil     Bsp2       1\n",
       "11    Felix     Bsp2       3\n",
       "12    Alice     Bsp3       1\n",
       "13      Bob     Bsp3       2\n",
       "14  Charlie     Bsp3       3\n",
       "15     Dora     Bsp3       4\n",
       "16     Emil     Bsp3       5\n",
       "17    Felix     Bsp3       1\n",
       "18    Alice     Bsp4       6\n",
       "19      Bob     Bsp4       5\n",
       "20  Charlie     Bsp4       4\n",
       "21     Dora     Bsp4       3\n",
       "22     Emil     Bsp4       2\n",
       "23    Felix     Bsp4       3\n",
       "24    Alice     Bsp5       2\n",
       "25      Bob     Bsp5       3\n",
       "26  Charlie     Bsp5       4\n",
       "27     Dora     Bsp5       5\n",
       "28     Emil     Bsp5       6\n",
       "29    Felix     Bsp5       3\n",
       "30    Alice     Bsp6       4\n",
       "31      Bob     Bsp6       5\n",
       "32  Charlie     Bsp6       6\n",
       "33     Dora     Bsp6       1\n",
       "34     Emil     Bsp6       2\n",
       "35    Felix     Bsp6       2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_melted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 36 entries, 0 to 35\n",
      "Data columns (total 3 columns):\n",
      " #   Column    Non-Null Count  Dtype \n",
      "---  ------    --------------  ----- \n",
      " 0   Person    36 non-null     object\n",
      " 1   Category  36 non-null     object\n",
      " 2   Points    36 non-null     int64 \n",
      "dtypes: int64(1), object(2)\n",
      "memory usage: 992.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "df_melted.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24020 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# vs.\n",
    "df.set_index('Person').plot(kind='bar', figsize=(10, 6), xlabel='Person', ylabel='Points')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Es gibt aber auch Situationen, in denen ein Balkendiagramm nicht geeignet ist. Siehe zum Beispiel:"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 60488 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(from Forstner SS2022; JKU)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In so einem Fall würde man ein **Histogramm** verwenden (siehe nächstes Notebook)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tortendiagramm / Donut-Diagramm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wir kommen nun zu einem sehr ähnlichen Diagrammtyp im Vergleich zu den Balkendiagrammen."
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 260660 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(from https://www.jku.at/studium/studienarten/bachelordiplom/ba-artificial-intelligence/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Eigenschaften eines Torten- bzw. Donut-Diagramms**:\n",
    "* Kreissegmente stehen im Verhältnis zum Gesamtwert der Daten\n",
    "* Darstellung relativer Anteile (oft in Prozent) gut geeignet (360° = 100%), absolute Werte seltener\n",
    "* Kann als Torten-Diagramm (kompletter Kreis) oder als Donut-Diagramm (mit kreisförmiger Aussparung in der Mitte) dargestellt werden\n",
    "* Farben zur Unterscheidung der Segmente werden häufig verwendet\n",
    "* Zusätzliche Beschriftungen:\n",
    "    * Titel\n",
    "    * Werte in oder neben den Segmenten (oft in Prozent)\n",
    "    * Legende zur Identifikation der Segmente\n",
    "* Ideal für eine begrenzte Anzahl von Kategorien, da zu viele Segmente unübersichtlich wirken\n",
    "* Donut-Diagramme bieten zusätzlichen Platz in der Mitte für Beschriftungen oder weiterführende Informationen"
   ]
  },
  {
   "attachments": {
    "nr-wahl-sitze-nationalrat.jpg": {
     "image/jpeg": "[encoded data, 12240 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![nr-wahl-sitze-nationalrat.jpg](attachment:nr-wahl-sitze-nationalrat.jpg)\n",
    "\n",
    "(von https://www.vienna.at/specials/nationalratswahl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Was fällt uns hier auf? Warum haben wir hier nur einen Halbkreis?"
   ]
  },
  {
   "attachments": {
    "file77k5g4qe5r8q0j1i61v-768x768.jpg": {
     "image/jpeg": "[encoded data, 84300 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![file77k5g4qe5r8q0j1i61v-768x768.jpg](attachment:file77k5g4qe5r8q0j1i61v-768x768.jpg)\n",
    "\n",
    "(von https://www.vienna.at/sitzplan-im-nationalrat-steht-fest-wer-sitzt-neben-wem/6392113)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36532 characters]",
      "text/plain": [
       "<Figure size 600x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Wir können natürlich auch solche Diagramme mit Python erstellen\n",
    "\n",
    "# Fiktive Daten\n",
    "labels = ['Kategorie A', 'Kategorie B', 'Kategorie C', 'Kategorie D']\n",
    "sizes = [25, 35, 20, 20]\n",
    "colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']\n",
    "\n",
    "# Erstellen eines Tortendiagramms\n",
    "plt.figure(figsize=(6,6))\n",
    "plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)\n",
    "plt.title(\"Tortendiagramm Beispiel\")\n",
    "plt.axis('equal')  # Sicherstellen, dass das Diagramm kreisförmig ist\n",
    "\n",
    "# Anzeigen des Tortendiagramms\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39820 characters]",
      "text/plain": [
       "<Figure size 600x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Erstellen eines Donut-Diagramms\n",
    "plt.figure(figsize=(6,6))\n",
    "plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90, wedgeprops={'width':0.4}) # Unterschied: wedgeprops={'width':0.4} !\n",
    "plt.title(\"Donut-Diagramm Beispiel\")\n",
    "plt.axis('equal')  # Sicherstellen, dass das Diagramm kreisförmig ist\n",
    "\n",
    "# Anzeigen des Donut-Diagramms\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lineplot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Nun besprechen wir eine Möglichkeit, Daten zu visualisieren, welche mitunter auch geeignet ist, Funktionen zu plotten."
   ]
  },
  {
   "attachments": {
    "lineplot_29_0.png": {
     "image/png": "[encoded data, 124248 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![lineplot_29_0.png](attachment:lineplot_29_0.png)\n",
    "\n",
    "(von https://seaborn.pydata.org/generated/seaborn.lineplot.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Eigenschaften eines Line Plots**:\n",
    "* Darstellung von Datenpunkten als Linie, die durch Verbindung der Punkte die Veränderung einer Variablen über eine kontinuierliche Dimension (oft Zeit) zeigt\n",
    "* Für nicht-kontinuierliche Daten ist ein Line-Plot sehr kontraproduktiv, da hier dann Werte dazwischen erwartet werden.\n",
    "* Eignet sich besonders gut zur Visualisierung von Trends, Verläufen oder zeitlichen Entwicklungen\n",
    "* Kann sowohl absolute als auch relative Werte darstellen\n",
    "* Gut geeignet für den Vergleich mehrerer Datensätze, indem mehrere Linien in einem Diagramm verwendet werden\n",
    "* Zusätzliche Beschriftungen:\n",
    "    * Titel\n",
    "    * Achsenbeschriftung (x-Achse für unabhängige Variable, y-Achse für abhängige Variable)\n",
    "    * Legende zur Identifikation mehrerer Linien\n",
    "    * Markierung der Datenpunkte kann hinzugefügt werden (z.B. Kreise, Punkte)\n",
    "* Sehr intuitiv\n",
    "* Unübersichtlich bei zu vielen Linien\n",
    "* Empfindlich gegenüber Ausreißern"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 92148 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(from Marc Streit, VA WS2022; JKU)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 111296 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 70580 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Nun zeigen wir, wie wir in Python einen Lineplot erstellen können\n",
    "\n",
    "data = {\n",
    "    'Year': [2018, 2019, 2020, 2021, 2022],\n",
    "    'Product A': [10, 20, 30, 25, 40],\n",
    "    'Product B': [15, 25, 20, 35, 45],\n",
    "    'Product C': [20, 30, 25, 30, 50]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "plt.figure(figsize=(8, 6))\n",
    "\n",
    "plt.plot(df['Year'], df['Product A'], label='Product A', marker='o') # kreis\n",
    "plt.plot(df['Year'], df['Product B'], label='Product B', marker='s') # square\n",
    "plt.plot(df['Year'], df['Product C'], label='Product C', marker='d') # diamond\n",
    "\n",
    "plt.title(\"Sales of Products Over Time\", size=15)\n",
    "plt.xlabel(\"Year\", size=12)\n",
    "plt.ylabel(\"Sales\", size=12)\n",
    "\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39000 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Wir können natürlich so auch Funktionen plotten\n",
    "\n",
    "def f(x):\n",
    "    return 1/(1+np.exp(-x))\n",
    "\n",
    "def f_prime(x):\n",
    "    return f(x) * (1 - f(x))\n",
    "\n",
    "x = np.linspace(-20, 20, 1000) # 1000 Werte zwischen -10 und 10\n",
    "y = f(x)\n",
    "y_prime = f_prime(x)\n",
    "\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.plot(x, y, label='f(x) = 1/(1+exp(-x))', color='red', marker='') # hier ist das Marker-Attribut eher kontraproduktiv\n",
    "plt.plot(x, y_prime, label=\"f'(x) = f(x) * (1 - f(x))\", color='blue', marker='') # hier ist das Marker-Attribut eher kontraproduktiv\n",
    "plt.title('Sigmoid Function', size=15)\n",
    "plt.xlabel('x', size=12)\n",
    "plt.ylabel('f(x)', size=12)\n",
    "plt.grid()\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Wir können uns auch ein Konfidenzintervall für eine Linie anzeigen lassen\n",
    "flights = sns.load_dataset('flights')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "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>year</th>\n",
       "      <th>month</th>\n",
       "      <th>passengers</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1949</td>\n",
       "      <td>Jan</td>\n",
       "      <td>112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1949</td>\n",
       "      <td>Feb</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1949</td>\n",
       "      <td>Mar</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1949</td>\n",
       "      <td>Apr</td>\n",
       "      <td>129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1949</td>\n",
       "      <td>May</td>\n",
       "      <td>121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>1960</td>\n",
       "      <td>Aug</td>\n",
       "      <td>606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>1960</td>\n",
       "      <td>Sep</td>\n",
       "      <td>508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>1960</td>\n",
       "      <td>Oct</td>\n",
       "      <td>461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>1960</td>\n",
       "      <td>Nov</td>\n",
       "      <td>390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>1960</td>\n",
       "      <td>Dec</td>\n",
       "      <td>432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>144 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     year month  passengers\n",
       "0    1949   Jan         112\n",
       "1    1949   Feb         118\n",
       "2    1949   Mar         132\n",
       "3    1949   Apr         129\n",
       "4    1949   May         121\n",
       "..    ...   ...         ...\n",
       "139  1960   Aug         606\n",
       "140  1960   Sep         508\n",
       "141  1960   Oct         461\n",
       "142  1960   Nov         390\n",
       "143  1960   Dec         432\n",
       "\n",
       "[144 rows x 3 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 49392 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.lineplot(data=flights, x=\"year\", y=\"passengers\", marker=\"o\")\n",
    "plt.title(\"Passenger Numbers Over Time\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Welche Werte werden hier angezeigt?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get Data from 1960\n",
    "flights_1960 = flights[flights['year'] == 1960]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "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>year</th>\n",
       "      <th>month</th>\n",
       "      <th>passengers</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>132</th>\n",
       "      <td>1960</td>\n",
       "      <td>Jan</td>\n",
       "      <td>417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>133</th>\n",
       "      <td>1960</td>\n",
       "      <td>Feb</td>\n",
       "      <td>391</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>134</th>\n",
       "      <td>1960</td>\n",
       "      <td>Mar</td>\n",
       "      <td>419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>135</th>\n",
       "      <td>1960</td>\n",
       "      <td>Apr</td>\n",
       "      <td>461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>1960</td>\n",
       "      <td>May</td>\n",
       "      <td>472</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137</th>\n",
       "      <td>1960</td>\n",
       "      <td>Jun</td>\n",
       "      <td>535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>1960</td>\n",
       "      <td>Jul</td>\n",
       "      <td>622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>1960</td>\n",
       "      <td>Aug</td>\n",
       "      <td>606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>1960</td>\n",
       "      <td>Sep</td>\n",
       "      <td>508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>1960</td>\n",
       "      <td>Oct</td>\n",
       "      <td>461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>1960</td>\n",
       "      <td>Nov</td>\n",
       "      <td>390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>1960</td>\n",
       "      <td>Dec</td>\n",
       "      <td>432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     year month  passengers\n",
       "132  1960   Jan         417\n",
       "133  1960   Feb         391\n",
       "134  1960   Mar         419\n",
       "135  1960   Apr         461\n",
       "136  1960   May         472\n",
       "137  1960   Jun         535\n",
       "138  1960   Jul         622\n",
       "139  1960   Aug         606\n",
       "140  1960   Sep         508\n",
       "141  1960   Oct         461\n",
       "142  1960   Nov         390\n",
       "143  1960   Dec         432"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights_1960"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(5714)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights_1960['passengers'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(476.1666666666667)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights_1960['passengers'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(77.73712501797714)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights_1960['passengers'].std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(43.98391905405356)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Confidence interval (95%)\n",
    "\n",
    "import scipy.stats as stats\n",
    "\n",
    "mean = flights_1960['passengers'].mean()\n",
    "std = flights_1960['passengers'].std()\n",
    "n = flights_1960['passengers'].count()\n",
    "z = 1.96 # 95% confidence interval\n",
    "\n",
    "confidence_interval = z * (std / np.sqrt(n)) # Formel nicht unbedingt relevant, aber wichtig, dass man sowas machen kann\n",
    "\n",
    "confidence_interval"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Händische Berechnung:\n",
    "\n",
    "$$ci = \\bar{x} \\pm \\frac{\\sigma}{\\sqrt{n}} = 476 \\pm \\frac{77}{\\sqrt{12}} = 476 \\pm 43.98$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Korrelationskoeffizient"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Misst den linearen Zusammenhang zwischen 2 Datenreihen\n",
    "* verschiedene Arten, wir verwenden (Bravais-Pearson Correlation Coefficient)\n",
    "* Hat einen Wert zwischen $-1$ und $1$.\n",
    "    * -1 bedeutet, es herrscht perfekte negative Korrelation\n",
    "    * +1 bedeutet, es herrscht perfekte positive Korrelation\n",
    "    * 0 bedeutet, es herrscht keine lineare Korrelation (kann aber trotzdem quadratisch sein)"
   ]
  },
  {
   "attachments": {
    "Correlation_examples2.svg.png": {
     "image/png": "[encoded data, 449988 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Correlation_examples2.svg.png](attachment:Correlation_examples2.svg.png)\n",
    "\n",
    "(from https://en.wikipedia.org/wiki/Pearson_correlation_coefficient#/media/File:Correlation_examples2.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Berechnung für Dataset $X=((x_1,y_1), (x_2,y_2), \\ldots, (x_n,y_n))$:\n",
    "\n",
    "\\begin{align*}\n",
    "    r_{xy} &= \\frac{\\textrm{Cov}(x,y)}{\\sigma(x) \\cdot \\sigma(y)}\\\\\n",
    "    &= \\frac{\\frac{1}{n}\\sum_{i=1}^{n}(x_i-\\overline{x})\\cdot (y_i-\\overline{y})}{\\sqrt{\\textrm{Var}(x)}\\cdot \\sqrt{\\textrm{Var}(y)}}\\\\\n",
    "    &= \\frac{\\frac{1}{n}\\sum_{i=1}^{n}(x_i-\\overline{x})\\cdot (y_i-\\overline{y})}{\\sqrt{\\textrm{Var}(x)\\cdot \\textrm{Var}(y)}} \\\\\n",
    "    &= \\frac{\\frac{1}{n}\\sum_{i=1}^{n}(x_i-\\overline{x})\\cdot (y_i-\\overline{y})}{\\sqrt{(\\frac{1}{n}\\sum_{i=1}^{n}(x_i-\\overline{x})^2)\\cdot (\\frac{1}{n}\\sum_{i=1}^{n}(y_i-\\overline{y})^2)}}\\\\\n",
    "    &= \\frac{\\sum_{i=1}^{n}(x_i-\\overline{x})\\cdot (y_i-\\overline{y})}{\\sqrt{(\\sum_{i=1}^{n}(x_i-\\overline{x})^2)\\cdot (\\sum_{i=1}^{n}(y_i-\\overline{y})^2)}}\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Beispiel:\n",
    "| Weight (kg) | Length (cm) |\n",
    "|-------------|-------------|\n",
    "| 3.63        | 53.1        |\n",
    "| 3.02        | 49.7        |\n",
    "| 3.82        | 48.4        |\n",
    "| 3.42        | 54.2        |\n",
    "| 3.59        | 54.9        |\n",
    "| 2.87        | 43.7        |\n",
    "| 3.03        | 47.2        |\n",
    "| 3.46        | 45.2        |\n",
    "| 3.36        | 54.4        |\n",
    "| 3.30        | 50.4        |\n",
    "\n",
    "Berechne den Korrelationskoeffizienten!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lösung?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plot der Daten:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23388 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "weight = [3.63, 3.02, 3.82, 3.42, 3.59, 2.87, 3.03, 3.46, 3.36, 3.30]\n",
    "length = [53.1, 49.7, 48.4, 54.2, 54.9, 43.7, 47.2, 45.2, 54.4, 50.4]\n",
    "\n",
    "data = pd.DataFrame({'Weight': weight, 'Length': length})\n",
    "\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.scatter(length, weight)\n",
    "plt.title(\"Scatterplot of Length and Weight\")\n",
    "plt.xlabel(\"Length\")\n",
    "plt.ylabel(\"Weight\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Vergleich mit der bisher bekannten Methode (ergänzt um Korrelationskoeffizient)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# regression including plot and its function without the confidence interval\n",
    "def linear_regression(data: pd.DataFrame, x: str, y: str) -> None:\n",
    "    \"\"\"\n",
    "    Perform linear regression on data. Makes a legend cell which contains the function of the regression.\n",
    "    \"\"\"\n",
    "    # Create linear regression object\n",
    "    regr = lm.LinearRegression()\n",
    "\n",
    "    # Train the model using the training sets\n",
    "    regr.fit(data[x].values.reshape(-1, 1), data[y])\n",
    "\n",
    "    # Make predictions using the testing set\n",
    "    y_pred = regr.predict(data[x].values.reshape(-1, 1))\n",
    "\n",
    "    # Calculate Pearson correlation coefficient\n",
    "    corr_coeff, _ = pearsonr(data[x], data[y])\n",
    "\n",
    "    # Plot outputs\n",
    "    plt.scatter(data[x], data[y])\n",
    "    plt.plot(data[x], y_pred, color='blue', linewidth=3)\n",
    "\n",
    "    # Add legend\n",
    "    plt.legend([\n",
    "        f\"y = {regr.coef_[0]:.2f}x + {regr.intercept_:.2f}\\n\"\n",
    "        f\"r = {corr_coeff:.2f}\"\n",
    "    ])\n",
    "\n",
    "    # add axis labels\n",
    "    plt.xlabel(x)\n",
    "    plt.ylabel(y)\n",
    "\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22676 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "linear_regression(data, 'Length', 'Weight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Radar Diagramm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Eine weitere Möglichkeit, mehrere Features pro Label/Kategorie darzustellen, ist das sogenannte Radar-Plot."
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 273920 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Eigenschaften eines Radar-Diagramms**:\n",
    "* Darstellung mehrerer Variablen in einem Netzdiagramm mit radialen Achsen\n",
    "* Jeder Punkt auf den Achsen repräsentiert einen Wert, der durch Linien miteinander verbunden wird\n",
    "* Gut geeignet, um mehrere Kategorien oder Objekte visuell miteinander zu vergleichen\n",
    "* Normalerweise für relative Werte verwendet (z.B. Prozentsätze oder normierte Werte)\n",
    "* Jeder Datensatz wird als eine Linie im Diagramm dargestellt, oft durch Farben unterschieden\n",
    "* Weitere Beschriftungen:\n",
    "    * Titel\n",
    "    * Beschriftung der Achsen (jedes Segment beschreibt eine Kategorie)\n",
    "    * Legende zur Identifikation der verschiedenen Datensätze\n",
    "* Bei zu vielen Kategorien kann das Diagramm unübersichtlich werden\n",
    "* Erneut eine Categorical Colormap zu verwenden\n",
    "* Kann verzerrend sein, weil mehr Farbe (Kantenlänge) wenn weiter außen."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Nun benötigen wir das `plotly` Package.\n",
    "\n",
    "Dieses lässt sich durch `conda install -c conda-forge plotly` installieren."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.express as px\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "hovertemplate": "r=%{r}<br>theta=%{theta}<extra></extra>",
         "legendgroup": "",
         "line": {
          "color": "#636efa",
          "dash": "solid"
         },
         "marker": {
          "symbol": "circle"
         },
         "mode": "lines",
         "name": "",
         "r": [
          1,
          5,
          2,
          2,
          3,
          1
         ],
         "showlegend": false,
         "subplot": "polar",
         "theta": [
          "processing cost",
          "mechanical properties",
          "chemical stability",
          "thermal stability",
          "device integration",
          "processing cost"
         ],
         "type": "scatterpolar"
        }
       ],
       "layout": {
        "legend": {
         "tracegroupgap": 0
        },
        "margin": {
         "t": 60
        },
        "polar": {
         "angularaxis": {
          "direction": "clockwise",
          "rotation": 90
         },
         "domain": {
          "x": [
           0,
           1
          ],
          "y": [
           0,
           1
          ]
         }
        },
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "data = {\n",
    "    'r':[1, 5, 2, 2, 3],\n",
    "    'theta':['processing cost','mechanical properties','chemical stability',\n",
    "           'thermal stability', 'device integration']\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "fig = px.line_polar(df, r='r', theta='theta', line_close=True)\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parallele Achsen Diagramm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ein sehr ähnliches Konzept wie die Radar Diagramme sind die sogenannten Parallel-Coordinates Diagramme."
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 595204 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(from Marc Streit, VA WS22; JKU)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Eigenschaften von Parallel Coordinates Plots**:\n",
    "* Jede Variable wird als senkrechte Achse dargestellt, Werte werden durch Linien zwischen den Achsen verbunden\n",
    "* Gut geeignet, um mehrere Variablen über verschiedene Datensätze hinweg zu vergleichen\n",
    "* Jede Linie repräsentiert einen Datensatz und verbindet die Werte auf den entsprechenden Achsen\n",
    "* Kann sowohl für absolute als auch relative Werte verwendet werden (Normalisierung nicht zwingend notwendig, weil jede Achse eine eigene Skalierung hat)\n",
    "* Farbige Linien helfen, unterschiedliche Datensätze voneinander zu unterscheiden\n",
    "* Weitere Beschriftungen:\n",
    "    * Titel\n",
    "    * Achsenbeschriftung (jede vertikale Achse steht für eine Variable)\n",
    "    * Legende zur Identifikation verschiedener Datensätze\n",
    "* Kann sowohl für große als auch kleine Datensätze verwendet werden, aber bei vielen Variablen oder Datensätzen kann es komplex werden (Overplotting)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 1046404 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(from Marc Streit, VA WS22; JKU)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "dimensions": [
          {
           "label": "F1",
           "values": [
            0.1,
            0.4,
            0.5,
            0.9
           ]
          },
          {
           "label": "F2",
           "values": [
            1.1,
            3.4,
            2.2,
            1.5
           ]
          },
          {
           "label": "F3",
           "values": [
            2,
            1.6,
            3.3,
            4.5
           ]
          },
          {
           "label": "F4",
           "values": [
            4.1,
            2.5,
            3.9,
            2.2
           ]
          }
         ],
         "domain": {
          "x": [
           0,
           1
          ],
          "y": [
           0,
           1
          ]
         },
         "line": {
          "color": [
           0.1,
           0.4,
           0.5,
           0.9
          ],
          "coloraxis": "coloraxis"
         },
         "name": "",
         "type": "parcoords"
        }
       ],
       "layout": {
        "coloraxis": {
         "cmid": 2,
         "colorbar": {
          "title": {
           "text": "F1"
          }
         },
         "colorscale": [
          [
           0,
           "rgb(0, 147, 146)"
          ],
          [
           0.16666666666666666,
           "rgb(114, 170, 161)"
          ],
          [
           0.3333333333333333,
           "rgb(177, 199, 179)"
          ],
          [
           0.5,
           "rgb(241, 234, 200)"
          ],
          [
           0.6666666666666666,
           "rgb(229, 185, 173)"
          ],
          [
           0.8333333333333334,
           "rgb(217, 137, 148)"
          ],
          [
           1,
           "rgb(208, 88, 126)"
          ]
         ]
        },
        "legend": {
         "tracegroupgap": 0
        },
        "margin": {
         "t": 60
        },
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import plotly.express as px\n",
    "import pandas as pd\n",
    "\n",
    "# Beispiel-Datensatz erstellen\n",
    "data = {\n",
    "    'Feature 1': [0.1, 0.4, 0.5, 0.9],\n",
    "    'Feature 2': [1.1, 3.4, 2.2, 1.5],\n",
    "    'Feature 3': [2.0, 1.6, 3.3, 4.5],\n",
    "    'Feature 4': [4.1, 2.5, 3.9, 2.2],\n",
    "    'Label': ['A', 'B', 'C', 'D']\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "# Parallel Coordinates Plot erstellen\n",
    "fig = px.parallel_coordinates(df,\n",
    "                              color='Feature 1',  # Farbcodierung nach 'Feature 1'\n",
    "                              labels={\"Feature 1\": \"F1\", \"Feature 2\": \"F2\", \"Feature 3\": \"F3\", \"Feature 4\": \"F4\"},\n",
    "                              dimensions=['Feature 1', 'Feature 2', 'Feature 3', 'Feature 4'],\n",
    "                              color_continuous_scale=px.colors.diverging.Tealrose,\n",
    "                              color_continuous_midpoint=2)\n",
    "\n",
    "# Visualisierung anzeigen\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Heatmap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Auch bei dieser Art von Visualisierung ist es möglich, mehrere Labels mit mehreren Kategorien und deren Werte zu plotten. "
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 82032 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(von https://seaborn.pydata.org/generated/seaborn.heatmap.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Eigenschaften einer Heatmap**:\n",
    "* Darstellung von Daten in einem rechteckigen Gitter, bei dem die Zellen durch Farben die Werte repräsentieren\n",
    "* Sequentielle Colormap\n",
    "* Ideal zur Visualisierung von Datenmatrizen, um Muster, Korrelationen oder Anomalien zu erkennen\n",
    "* Besonders gut geeignet für große Datenmengen, die schwer in herkömmlichen Diagrammen darstellbar sind\n",
    "* Kann sowohl für absolute als auch für relative Werte verwendet werden\n",
    "* Weitere Beschriftungen:\n",
    "    * Titel\n",
    "    * Achsenbeschriftung (x- und y-Achsen für Kategorien oder Zeiträume)\n",
    "    * Farbskala oder Legende zur Interpretation der Farbwerte\n",
    "* Oft in Kombination mit zusätzlichen Annotationen wie Zahlen in den Zellen für präzise Werte\n",
    "* Schnelle Mustererkennung möglich\n",
    "* Gut für Korrelationsanalysen zBsp.: Korrelationsmatrix\n",
    "* Stark abhängig von Colormap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "glue = sns.load_dataset(\"glue\").pivot(index=\"Model\", columns=\"Task\", values=\"Score\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "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>Task</th>\n",
       "      <th>CoLA</th>\n",
       "      <th>MNLI</th>\n",
       "      <th>MRPC</th>\n",
       "      <th>QNLI</th>\n",
       "      <th>QQP</th>\n",
       "      <th>RTE</th>\n",
       "      <th>SST-2</th>\n",
       "      <th>STS-B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Model</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BERT</th>\n",
       "      <td>60.5</td>\n",
       "      <td>86.7</td>\n",
       "      <td>89.3</td>\n",
       "      <td>92.7</td>\n",
       "      <td>72.1</td>\n",
       "      <td>70.1</td>\n",
       "      <td>94.9</td>\n",
       "      <td>87.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BiLSTM</th>\n",
       "      <td>11.6</td>\n",
       "      <td>65.6</td>\n",
       "      <td>81.8</td>\n",
       "      <td>74.6</td>\n",
       "      <td>62.5</td>\n",
       "      <td>57.4</td>\n",
       "      <td>82.8</td>\n",
       "      <td>70.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BiLSTM+Attn</th>\n",
       "      <td>18.6</td>\n",
       "      <td>67.6</td>\n",
       "      <td>83.9</td>\n",
       "      <td>74.3</td>\n",
       "      <td>60.1</td>\n",
       "      <td>58.4</td>\n",
       "      <td>83.0</td>\n",
       "      <td>72.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BiLSTM+CoVe</th>\n",
       "      <td>18.5</td>\n",
       "      <td>65.4</td>\n",
       "      <td>78.7</td>\n",
       "      <td>70.8</td>\n",
       "      <td>60.6</td>\n",
       "      <td>52.7</td>\n",
       "      <td>81.9</td>\n",
       "      <td>64.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BiLSTM+ELMo</th>\n",
       "      <td>32.1</td>\n",
       "      <td>67.2</td>\n",
       "      <td>84.7</td>\n",
       "      <td>75.5</td>\n",
       "      <td>61.1</td>\n",
       "      <td>57.4</td>\n",
       "      <td>89.3</td>\n",
       "      <td>70.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ERNIE</th>\n",
       "      <td>75.5</td>\n",
       "      <td>92.3</td>\n",
       "      <td>93.9</td>\n",
       "      <td>97.3</td>\n",
       "      <td>75.2</td>\n",
       "      <td>92.6</td>\n",
       "      <td>97.8</td>\n",
       "      <td>93.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RoBERTa</th>\n",
       "      <td>67.8</td>\n",
       "      <td>90.8</td>\n",
       "      <td>92.3</td>\n",
       "      <td>95.4</td>\n",
       "      <td>74.3</td>\n",
       "      <td>88.2</td>\n",
       "      <td>96.7</td>\n",
       "      <td>92.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>T5</th>\n",
       "      <td>71.6</td>\n",
       "      <td>92.2</td>\n",
       "      <td>92.8</td>\n",
       "      <td>96.9</td>\n",
       "      <td>75.1</td>\n",
       "      <td>92.8</td>\n",
       "      <td>97.5</td>\n",
       "      <td>93.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Task         CoLA  MNLI  MRPC  QNLI   QQP   RTE  SST-2  STS-B\n",
       "Model                                                        \n",
       "BERT         60.5  86.7  89.3  92.7  72.1  70.1   94.9   87.6\n",
       "BiLSTM       11.6  65.6  81.8  74.6  62.5  57.4   82.8   70.3\n",
       "BiLSTM+Attn  18.6  67.6  83.9  74.3  60.1  58.4   83.0   72.8\n",
       "BiLSTM+CoVe  18.5  65.4  78.7  70.8  60.6  52.7   81.9   64.4\n",
       "BiLSTM+ELMo  32.1  67.2  84.7  75.5  61.1  57.4   89.3   70.3\n",
       "ERNIE        75.5  92.3  93.9  97.3  75.2  92.6   97.8   93.0\n",
       "RoBERTa      67.8  90.8  92.3  95.4  74.3  88.2   96.7   92.2\n",
       "T5           71.6  92.2  92.8  96.9  75.1  92.8   97.5   93.1"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "glue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: xlabel='Task', ylabel='Model'>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 79992 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.heatmap(glue, annot=True, fmt=\".1f\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: xlabel='Task', ylabel='Model'>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 78752 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.heatmap(glue, annot=True, fmt=\".1f\", cmap='viridis')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: xlabel='Task', ylabel='Model'>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 47532 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.heatmap(glue, annot=glue.rank(axis=\"columns\")) # Auch Rank möglich!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: title={'center': 'Scores by Model and Task'}, xlabel='Model', ylabel='Score'>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 34592 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Grouped Barplot\n",
    "\n",
    "glue.plot(kind='bar', figsize=(10, 6), xlabel='Model', ylabel='Score', title='Scores by Model and Task', rot=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Was sieht besser aus?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Aufgabe"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Lade das Cars Dataset `cars.csv`\n",
    "* Führe zuerst notwendige Vorverarbeitungsschritte durch (sind (viele) Werte leer, unmöglich, Normalisieren, etc.) und Informieren Sie sich auch über das Dataset (zBsp. über https://www.kaggle.com/datasets/goyalshalini93/car-data)\n",
    "* Geben Sie verschiedene Zentral und Streumaße an für Features, wo Sie es für notwendig halten\n",
    "* Testen Sie die verschiedenen, bisher gelernten, Visualisierungsmöglichkeiten und Visualisieren Sie die Daten in einigen *sinnvollen* Grafiken\n",
    "* Dokumentieren Sie alle Ergebnisse!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dsai",
   "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.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
