{
 "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 1: Allgemeine Prinzipien</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": [
    "## Allgemeine Ziele und wichtige Eigenschaften"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hier möchten wir uns mit allen möglichen Arten von Visualisierungen unterhalten.\n",
    "\n",
    "Dabei werden wir hauptsächlich:\n",
    "* Lernen, wie gute Visualisierungen aussehen.\n",
    "* Visualisierungen selber erstellen\n",
    "* Visualisierungen interpretieren"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Als Motivation möchte ich folgende Grafik zu den tödlichen Verkehrsunfällen der Fahrer von PKW's aus dem Jahr 2023 verwenden. Sie stammt von der Statistik Austria und kann [hier](https://www.statistik.at/statistiken/tourismus-und-verkehr/unfaelle/strassenverkehrsunfaelle) bzw. [hier](https://www.statistik.at/fileadmin/pages/80/Strassenverkehrsunfaelle_2023.ods) nachgelesen werden (Blatt WD_T14)."
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 26348 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Somit waren $\\frac{92}{129}=71.3\\%$ der toten Personen angeschnallt, jedoch nur $\\frac{37}{129}=28.7\\%$ nicht angeschnallt. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Was sagt uns diese Grafik/Statistik? Was sagt sie uns nicht?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Beispiele von schlechten Diagrammen/Visualisierungen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Die folgenden Diagramme sind sinnbildlich für schlechte Diagramme. Was genau ist an ihnen schlecht?\n",
    "\n",
    "All non-cited images from (Forstner; Statistics for AI; SS2022; JKU)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 1890036 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 1192744 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 758616 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "attachments": {
    "image-2.png": {
     "image/png": "[encoded data, 571340 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image-2.png](attachment:image-2.png)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 1060024 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 325268 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 2203744 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 420280 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Interessante Quellen für (schlechte) Visualisierungen:\n",
    "* [Spurious Correlations](https://tylervigen.com/spurious-correlations)\n",
    "* [WTF Visualizations](https://viz.wtf/)\n",
    "* [xkcd](https://xkcd.com/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DON'TS bei Visualisierungen:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1. Skala-Verzerrung**"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 2236512 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(Marc Streit VA 2022W; JKU)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 1602436 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(Marc Streit VA 2022W; JKU)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 621216 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(Marc Streit VA 2022W; JKU)"
   ]
  },
  {
   "attachments": {
    "image-2.png": {
     "image/png": "[encoded data, 267600 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image-2.png](attachment:image-2.png)\n",
    "\n",
    "Same Data, different Scale (Ward, Grinstein, Keim, 2011)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2. Unnötiger Chart Junk**"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 807488 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "Chart Junk vermeiden (Tim Brey)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**3. Nur gerechtfertigtes 3D**"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 998932 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(Marc Streit VA 2022W; JKU)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 340116 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)\n",
    "\n",
    "(Gehlenborg & Wong, Points of View, Nature Methods, 2012)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 37444 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Stellt folgendes dar:\n",
    "\n",
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4. Zu viele (oder falsche) Farben**"
   ]
  },
  {
   "attachments": {
    "poll-results-uglified-1-1.webp": {
     "image/webp": "[encoded data, 11864 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![poll-results-uglified-1-1.webp](attachment:poll-results-uglified-1-1.webp)\n",
    "\n",
    "(https://flowingdata.com/2009/06/15/6-easy-steps-to-make-your-graph-really-ugly/) (Geht noch viel schlimmer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DO'S bei Visualisierungen:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* \"Genau das Gegenteil der Don'ts\"\n",
    "* Insbesondere:\n",
    "    * Richtiger Diagrammtyp\n",
    "    * Einfach und Klar\n",
    "    * Klare Achsenbeschriftung\n",
    "    * Sinnvolle Farben (Farben nur, wenn notwendig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Viele Do's und Don'ts sind Diagrammspezifisch, diese behandeln wir später."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Was soll eine **gute** Visualisierung sein:\n",
    "* Neutral\n",
    "* Einfache Interpretation der Ergebnisse\n",
    "* Gut Lesbar\n",
    "* Verständlich\n",
    "* Aussagekräftig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Was soll sie **NICHT** sein?\n",
    "* Verzerrend und Irreführend (Skalierungen, etc. )\n",
    "* Überladen (zu viele Informationen gleichzeitig oder zu viele Datenpunkte)\n",
    "* Unleserlicht\n",
    "* Schlecht Beschriftet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Auflösung der Grafik über die Verkehrstoten"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 61680 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Grafik war **NICHT** falsch\n",
    "* Viele Infos unterschlagen\n",
    "* Andere Sichtweise:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|  |  **Verunglückt** | **davon tötlich** |\n",
    "| ------------ | :---------------: | :--------------: |\n",
    "| **angeschnallt** | **13805** | **92** |\n",
    "| **nicht angeschnallt** | **505** | **37** |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Wir teilen die Daten etwas anders auf und erhalten\n",
    "\n",
    "data = pd.DataFrame(\n",
    "    {\n",
    "    'tot' : [92, 37],\n",
    "    'nicht_tot'  : [13713, 468]\n",
    "    },\n",
    "    index=['angeschnallt','nicht_angeschnallt']\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Anteil der angeschnallten toten Autofahrer aller toten Autofahrer\n",
    "toetlich_angeschnallt = data.loc['angeschnallt','tot'] / data['tot'].sum()\n",
    "\n",
    "print(f\"{toetlich_angeschnallt*100} Prozent der tödlich Verunglückten waren angeschnallt.\")\n",
    "print(f\"{100-toetlich_angeschnallt*100} Prozent der tödlich Verunglückten waren nicht angeschnallt.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verhältnis der tötlich Verunglückten im vergleich zu allen Autofahrern\n",
    "angeschnallt = data.loc['angeschnallt', 'tot'] / data.loc['angeschnallt'].sum()\n",
    "nicht_angeschnallt = data.loc['nicht_angeschnallt', 'tot'] / data.loc['nicht_angeschnallt'].sum()\n",
    "\n",
    "print(f'{angeschnallt*1000} Promille aller verunglückten, angeschnallten PKW-Lenker waren für den Lenker tödlich.')\n",
    "print(f'{nicht_angeschnallt*1000} Promille aller verunglückten, nicht angeschnallten PKW-Lenker waren für den Lenker tödlich. Das ist {nicht_angeschnallt/angeschnallt} mal so viel!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Wir können nun auch dieses Verhältnis plotten.\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.bar(['angeschnallt', 'nicht angeschnallt'], [angeschnallt*1000, 1000*nicht_angeschnallt], width=0.8)\n",
    "\n",
    "plt.xlabel('Sicherheitsgurt Status')\n",
    "plt.ylabel('Tödliche Unfälle pro 1000 Unfälle')\n",
    "plt.title('Vergleich der tödlichen Unfälle zwischen angeschnallten und nicht angeschnallten Fahrern')\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Wahrscheinlichkeitsbaum"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 25356 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "attachments": {
    "image-2.png": {
     "image/png": "[encoded data, 25404 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image-2.png](attachment:image-2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Berechnung von A-L"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{align*}\n",
    "    A&=\\frac{129}{14310} = 0.009014675\\\\\n",
    "    B&=\\frac{14181}{14310} = 0.990985325\\\\\n",
    "    C&=\\frac{92}{129} = 0.713178295\\\\\n",
    "    D&=\\frac{37}{129} = 0.286821705\\\\\n",
    "    E&=\\frac{13713}{14181} = 0.966998096\\\\\n",
    "    F&=\\frac{468}{14181} = 0.033001904\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{align*}\n",
    "    G&=\\frac{13805}{14310} = 0.964709993\\\\\n",
    "    H&=\\frac{505}{14310} = 0.035290007\\\\\n",
    "    I&=\\frac{92}{13805} = 0.006664252\\\\\n",
    "    J&=\\frac{13713}{13805} = 0.993335748\\\\\n",
    "    K&=\\frac{37}{505} = 0.073267327\\\\\n",
    "    L&=\\frac{468}{505} = 0.926732673\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wir können außerdem die kombinierten Wahrscheinlichkeiten berechnen: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{align*}\n",
    "    P(\\text{tot}\\land\\text{angeschnallt}) &= A\\cdot C = G\\cdot I = 0.006429071\\\\\n",
    "    P(\\lnot\\text{tot}\\land\\text{angeschnallt}) &= B\\cdot E = G\\cdot J = 0.958280922\\\\\n",
    "    P(\\text{tot}\\land\\lnot\\text{angeschnallt}) &= A \\cdot D = H\\cdot K = 0.002585604\\\\\n",
    "    P(\\lnot\\text{tot}\\land\\lnot\\text{angeschnallt}) &= B\\cdot F = H \\cdot L = 0.032704403\n",
    "\\end{align*}"
   ]
  }
 ],
 "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.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
