{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cf39354b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    },
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Matplotlib-(Teil-2)\" data-toc-modified-id=\"Matplotlib-(Teil-2)-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Matplotlib (Teil 2)</a></span><ul class=\"toc-item\"><li><span><a href=\"#Wiederholung\" data-toc-modified-id=\"Wiederholung-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Wiederholung</a></span></li><li><span><a href=\"#Achsen-und-Gitterlinien\" data-toc-modified-id=\"Achsen-und-Gitterlinien-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>Achsen und Gitterlinien</a></span></li><li><span><a href=\"#Reihenfolge-der-Elemente-festlegen\" data-toc-modified-id=\"Reihenfolge-der-Elemente-festlegen-1.3\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>Reihenfolge der Elemente festlegen</a></span></li><li><span><a href=\"#Bilder-abspeichern\" data-toc-modified-id=\"Bilder-abspeichern-1.4\"><span class=\"toc-item-num\">1.4&nbsp;&nbsp;</span>Bilder abspeichern</a></span></li><li><span><a href=\"#Logarithmische-Skalierung-der-Achsen\" data-toc-modified-id=\"Logarithmische-Skalierung-der-Achsen-1.5\"><span class=\"toc-item-num\">1.5&nbsp;&nbsp;</span>Logarithmische Skalierung der Achsen</a></span></li><li><span><a href=\"#Scatter-Plots\" data-toc-modified-id=\"Scatter-Plots-1.6\"><span class=\"toc-item-num\">1.6&nbsp;&nbsp;</span>Scatter Plots</a></span></li><li><span><a href=\"#Vektorpfeile-mit-quiver\" data-toc-modified-id=\"Vektorpfeile-mit-quiver-1.7\"><span class=\"toc-item-num\">1.7&nbsp;&nbsp;</span>Vektorpfeile mit quiver</a></span></li><li><span><a href=\"#Flächen-füllen\" data-toc-modified-id=\"Flächen-füllen-1.8\"><span class=\"toc-item-num\">1.8&nbsp;&nbsp;</span>Flächen füllen</a></span></li><li><span><a href=\"#Zeichnen-von-2d-reellwertigen-Daten-(imshow-und-contour)\" data-toc-modified-id=\"Zeichnen-von-2d-reellwertigen-Daten-(imshow-und-contour)-1.9\"><span class=\"toc-item-num\">1.9&nbsp;&nbsp;</span>Zeichnen von 2d reellwertigen Daten (imshow und contour)</a></span></li></ul></li><li><span><a href=\"#3D-Plots\" data-toc-modified-id=\"3D-Plots-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>3D Plots</a></span><ul class=\"toc-item\"><li><span><a href=\"#Polygonzug-in-3D\" data-toc-modified-id=\"Polygonzug-in-3D-2.1\"><span class=\"toc-item-num\">2.1&nbsp;&nbsp;</span>Polygonzug in 3D</a></span></li><li><span><a href=\"#Fläche-in-3d-mit-plot_surface\" data-toc-modified-id=\"Fläche-in-3d-mit-plot_surface-2.2\"><span class=\"toc-item-num\">2.2&nbsp;&nbsp;</span>Fläche in 3d mit <strong>plot_surface</strong></a></span></li><li><span><a href=\"#Vektorpfeile-in-3d-mit-quiver\" data-toc-modified-id=\"Vektorpfeile-in-3d-mit-quiver-2.3\"><span class=\"toc-item-num\">2.3&nbsp;&nbsp;</span>Vektorpfeile in 3d mit <strong>quiver</strong></a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "275b821c",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Matplotlib (Teil 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1eba4225",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "#modus für Vorlesung\n",
    "%matplotlib qt5\n",
    "plt.rcParams['figure.figsize'] = (4., 4.)\n",
    "plt.rcParams['lines.linewidth'] = 3\n",
    "#modus für Export nach pdf\n",
    "#%matplotlib inline\n",
    "#plt.rcParams[\"figure.figsize\"] = (3.0, 2.0) #default figsize für Export nach pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "572395bc",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "from matplotlib.backends import backend_registry\n",
    "# interactive backends\n",
    "#backend_registry.list_builtin(matplotlib.backends.BackendFilter.INTERACTIVE)\n",
    "# noninteractive backends\n",
    "backend_registry.list_builtin(matplotlib.backends.BackendFilter.NON_INTERACTIVE)\n",
    "# all backends built into Matplotlib\n",
    "# backend_registry.list_builtin()\n",
    "# all registered backends\n",
    "#backend_registry.list_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8a37c35",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#plt.rcParams.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5999362",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "https://matplotlib.org/stable/gallery/color/named_colors.html"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "363d4398",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Wiederholung"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18c8ec7b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 3, layout='constrained', figsize=(7, 2.5))\n",
    "fig.suptitle('super titel')\n",
    "fig.set_facecolor('azure')\n",
    "\n",
    "ax[0].set_title('ax 1 title right', loc='right', color='purple', fontsize=8)\n",
    "ax[0].set_facecolor('navy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45a3a78b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "def f(t):\n",
    "    return t**2 * np.exp(-t**2)\n",
    "\n",
    "t = np.linspace(0, 3, 51)\n",
    "y = f(t)\n",
    "\n",
    "ax[0].plot(t, y, color='yellow', label='$t^2\\\\exp(-t^2)$')\n",
    "ax[0].legend();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90d5b90a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Achsen und Gitterlinien \n",
    "Hinzufügen von Achsenbezeichnungen.  __layout='constrained'__ in plt.subplots sorgt dafür, dass alles sichtbar bleibt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "629c7a0d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "ax[0].set_xlabel('t', color='red', fontsize=14)\n",
    "ax[0].set_ylabel('f(t)');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8781a3c0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "ax[1].set_facecolor('grey')\n",
    "ax[1].grid(color='white')\n",
    "ax[1].plot(y, t, color='gold', alpha=.5);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "281e0baf",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "phi = np.linspace(0, 2 * np.pi)\n",
    "ax[2].plot(np.cos(phi), np.sin(phi), 'm', zorder=1)\n",
    "ax[2].scatter(np.cos(phi[::5]), np.sin(phi[::5]), s=20 + 10 * phi[::5])\n",
    "ax[2].set_aspect('equal')\n",
    "# Setze die bottom und left Achsen (engl. spines) als x und y Achse\n",
    "ax[2].spines['bottom'].set_position('zero')\n",
    "ax[2].spines['left'].set_position('zero')\n",
    "# Entferne top und right spines\n",
    "ax[2].spines['top'].set_visible(False)\n",
    "ax[2].spines['right'].set_visible(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edf8da30",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "ax[2].set_xlim((-2, 2))\n",
    "ax[2].set_ylim((-2, 2))\n",
    "ax[2].set_xticks([-1, 0, 1])\n",
    "ax[2].set_yticks([-1, 0, 1]);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "958c867b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Reihenfolge der Elemente festlegen\n",
    "__zorder__ legt fest, in welcher Reihenfolge die Elemente gezeichnet werden."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09ad2ad3",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "x = np.linspace(0, 7)\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(x, np.sin(x), label='zorder=1', color='blue', zorder=1)\n",
    "plt.plot(x, np.sin(x + 1), label='zorder=3', color='orange', zorder=3)\n",
    "plt.axhline(0, label='zorder=2', color='lightgrey', zorder=2)\n",
    "\n",
    "plt.title('z order der Element')\n",
    "l = plt.legend(loc='upper right')\n",
    "l.set_zorder(2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f12cf7dc",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Position der Legende \n",
    "\n",
    "| Location String |  Location Code |\n",
    "| :- | :-: |\n",
    "|'best'|  0 |\n",
    "'upper right' |    1\n",
    "'upper left'  |   2\n",
    "'lower left'  |  3\n",
    "'lower right' |  4\n",
    "'right'       |  5\n",
    "'center left' |  6\n",
    "'center right'|  7\n",
    "'lower center'|  8\n",
    "'upper center'|  9\n",
    "'center'      |  10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bd8733b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Bilder abspeichern\n",
    "Bild in einer Datei (pdf, jpeg, ...) speichern:\n",
    "\n",
    "__savefig__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2dcc335",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig.savefig('bild1.pdf')  # das format wird anhand der Endung geraten"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b32353cf",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig.savefig('bild1.jpg', format='jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3eecf4fb",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig.canvas.get_supported_filetypes_grouped()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f1d7c23",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Logarithmische Skalierung der Achsen\n",
    "Verwendung logarithmischer Achsen:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01e3ea8f",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "x = np.linspace(1, 10, 21)\n",
    "y = 2**(-x)\n",
    "fig, ax = plt.subplots(1)\n",
    "ax.semilogy(x, y, '+');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a810e53",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Siehe auch <code>semilogx()</code> und <code>loglog()</code>, wenn die x-Achse bzw. beide Achsen logarithmisch skaliert werden sollen."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65dd67ae",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Scatter Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd565c02",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(3, 3))\n",
    "n = 7\n",
    "t = np.linspace(0, 2 * np.pi, n, endpoint=False)\n",
    "x = np.cos(t)\n",
    "y = np.sin(t)\n",
    "color = t\n",
    "# Punkte an Koordinate (x_i, y_i) s: Größe  c: Farbe \n",
    "ax.scatter(x, y, s=50 * t + 5, c=color);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a050e7f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Vektorpfeile mit quiver"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6754d223",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(3, 3))\n",
    "x, y, u, v = 0, 0, 1, .5\n",
    "\n",
    "ax.quiver(x, y, u, v)  # (x,y) Fusspunkt (u,v) Richtung\n",
    "ax.set_title('Quiver mit einem Pfeil')\n",
    "ax.axis([-2, 2, -2, 2])\n",
    "ax.grid()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4851257c",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(3, 3))\n",
    "x, y, u, v = 0, 0, 1, .5\n",
    "\n",
    "ax.quiver(x, y, u, v, angles='xy', scale_units='xy', scale=1)\n",
    "ax.set_title('Quiver mit einem Pfeil')\n",
    "ax.axis([-2, 2, -2, 2])\n",
    "ax.grid()\n",
    "ax.set_aspect('equal')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ddefa25",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(3, 3))\n",
    "n = 7\n",
    "t = np.linspace(0, 2 * np.pi, n, endpoint=False)\n",
    "x = np.cos(t)\n",
    "y = np.sin(t)\n",
    "u = np.sin(t)\n",
    "v = -np.cos(t)\n",
    "color = t\n",
    "# Punkte an Koordinate (x_i, y_i) s: Größe  c: Farbe \n",
    "ax.scatter(x, y, s=5 * t + 5, c=color, cmap='plasma');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d5c6f91",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "qv = ax.quiver(x, y, u, v, color, angles='xy', scale_units='xy',\n",
    "               scale=1, cmap='plasma')\n",
    "ax.set_title('Quiver mit vielen Pfeilen')\n",
    "ax.axis([-2, 2, -2, 2])\n",
    "ax.set_aspect('equal')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9367667b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig.colorbar(qv);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95ceb60c",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Der Parameter __cmap__ wählt die Colormap aus.  \n",
    "Link zu Colormaps (cmap) https://matplotlib.org/stable/tutorials/colors/colormaps.html"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92bc9371",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Flächen füllen\n",
    "\n",
    "__fill__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2be33e20",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1)\n",
    "ax.plot([0, 1, 0], [0, 0, 1])\n",
    "ax.axis(xmin=-.01, ymin=-.01);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44cb47d2",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1)\n",
    "ax.fill([0, 1, 0, 0], [0, 0, 1, 0], color='red')\n",
    "ax.axis(xmin=-.01, ymin=-.01);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0435aee",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "x = np.linspace(0, 1.8 * np.pi)\n",
    "y1 = np.sin(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6882177",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1)\n",
    "ax.fill(x, y1)\n",
    "ax.plot(x, y1, 'xr')\n",
    "ax.axis('equal');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3709e00",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Zeichnen von 2d reellwertigen Daten (imshow und contour)\n",
    "<code>imshow<code> zum darstellen von gepixelten Bildern"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fff7c0a3",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "i = np.arange(5)\n",
    "M = (-1)**(i[:, np.newaxis] + i)\n",
    "fig, ax = plt.subplots(1)\n",
    "ax.imshow(M, cmap='Greys');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08fb4032",
   "metadata": {},
   "outputs": [],
   "source": [
    "M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c60e555b",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close('all')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac4b4c29",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "from matplotlib.image import imread\n",
    "\n",
    "img = imread('bild1.jpg')\n",
    "img.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a60cf95",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.imshow(img)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5d5ea8a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "<code>contour</code> berechnet und zeichnet Höhenlinien auf einem  kartesichen Gitter\n",
    "\n",
    "```python\n",
    "    contour(x, y, z, levels)\n",
    "```\n",
    "<code>x, y</code> : Arrays mit den Koordinaten für den Wert z\n",
    "\n",
    "<code>levels</code> : Anzahl der Höhenlinine oder Werte für die Höhenlinien"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccc8e7c5",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "x = np.linspace(-2, 2)\n",
    "y = np.linspace(-2, 2)\n",
    "xx, yy = np.meshgrid(x, y)\n",
    "zz = xx**2 - yy**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3b51d93",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1)\n",
    "cs = ax.contour(x, y, zz, 5) # 5 Höhenlinien ausgewählt von matplotlib\n",
    "#cs.clabel()\n",
    "ax.axis('equal');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00aa6629",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1)\n",
    "cs = ax.contour(x, y, zz, [-1, 0, 1]) # 3 Höhenlinien zu Werten -1, 0 und 1\n",
    "#cs.clabel()\n",
    "ax.axis('equal');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94df2ad8",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 3D Plots"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "954abc92",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Polygonzug in 3D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf3e872b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# erzeugen ein 3D Koordinatensystem\n",
    "ax = plt.figure().add_subplot(projection='3d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17b0d2bd",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "x = np.random.rand(5)\n",
    "y = np.random.rand(5)\n",
    "z = np.random.randn(5)\n",
    "\n",
    "ax.plot(x, y, z, 'co-');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccb4fa33",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(4, 4))\n",
    "ax = fig.add_subplot(projection='3d')\n",
    "\n",
    "x, y = [-1, 1, 1, -1, -1], [-1, -1, 1, 1, -1] \n",
    "for z in (-1, 1):                       # Zwei Quadrate\n",
    "    ax.plot(x, y, [z] * 5, color=\"r\")\n",
    "for a, b in zip(x[:4], y[:4]):          # verbinde die vier Ecken der Quadrate\n",
    "    ax.plot([a, a], [b, b], [-1, 1], color=\"b\")\n",
    "    \n",
    "ax.set_box_aspect((1, 1, 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7f3dc58",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Fläche in 3d mit __plot_surface__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01a1b21e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1,\n",
    "                       2,\n",
    "                       layout='constrained',\n",
    "                       subplot_kw={'projection': '3d'})\n",
    "\n",
    "phi = np.linspace(0, 2 * np.pi, 12).reshape(-1, 1)\n",
    "theta = np.linspace(0, np.pi, 12).reshape(1, -1)\n",
    "theta = np.append(theta, np.nan) #nan Trick um ungewünschte Linien zu entfernen\n",
    "\n",
    "# Parametrisierung der Kugelfläche siehe https://de.wikipedia.org/wiki/Kugel\n",
    "x = np.cos(phi) * np.sin(theta)  # durch broadcasting sind x, y, z hier 2Darrays\n",
    "y = np.sin(phi) * np.sin(theta)\n",
    "z = (phi * 0 + 1) * np.cos(theta)\n",
    "\n",
    "ax[0].plot(x.flatten(), y.flatten(), z.flatten(), 'ro-', linewidth=1)\n",
    "ax[1].plot_surface(x, y, z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d86d3c49",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Vektorpfeile in 3d mit __quiver__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ea60bdf",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(5, 5))\n",
    "ax = fig.add_subplot(projection='3d')\n",
    "\n",
    "phi = np.linspace(0, 2 * np.pi, 6, endpoint=False).reshape(-1, 1)\n",
    "theta = np.linspace(0, np.pi, 6, endpoint=False).reshape(1, -1)\n",
    "\n",
    "# durch broadcasting sind u, v, w hier 2D arrays\n",
    "u = np.cos(phi) * np.sin(theta)  \n",
    "v = np.sin(phi) * np.sin(theta)\n",
    "w = np.ones_like(phi) * np.cos(theta)\n",
    "\n",
    "ax.quiver(0, 0, 0, u, v, w)\n",
    "ax.set_box_aspect((1, 1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0d50ba0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "ax.set_xlim(-2, 2)\n",
    "ax.set_ylim(-2, 2)\n",
    "ax.set_zlim(-2, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db43727c",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "plt.close('all')"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": false,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "340.267px"
   },
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
