Skip to content

Commit

Permalink
clarified subplots
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Dec 11, 2016
1 parent 9c5018b commit b65946c
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions 202 - Plots mit Matplotlib.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -113,7 +113,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -149,7 +149,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -207,7 +207,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -244,7 +244,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -306,7 +306,7 @@
"source": [
"## Mehrere Plots in einer Abbildung\n",
"\n",
"Eine Abbildung kann in mehrere `subplots` aufgeteilt werden:"
"Wir können eine Abbildung in ein Gitter mit mehreren Achsen aufteilen. Funktionsaufrufe wie `plt.plot` plotten dann immer in die zuletzt erstellte Achse:"
]
},
{
Expand All @@ -317,18 +317,46 @@
},
"outputs": [],
"source": [
"# Eine Abbildung mit 2x1 Subplots erstellen\n",
"fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=True)\n",
"# In beiden Subplots plotten\n",
"# Erstelle eine Abbildung mit Titel\n",
"fig = plt.figure()\n",
"fig.suptitle(\"Oszillation\")\n",
"\n",
"# Erstelle eine Achse in einem Gitter von 2 Zeilen und 1 Spalte\n",
"ax_upper = plt.subplot(211) # Abkürzung für die 1. Achse im Gitter\n",
"\n",
"# Funktionen plotten nun in diese Achse:\n",
"x = np.linspace(0, 2 * np.pi, 100)\n",
"axes[0].plot(x, np.sin(x), label=r'$\\sin(\\phi)$')\n",
"axes[1].plot(x, np.cos(x), label=r'$\\cos(\\phi)$')\n",
"# Die Abbildung konfigurieren\n",
"fig.suptitle('Oszillation')\n",
"axes[0].set_xlim(0, 2 * np.pi)\n",
"axes[1].set_xlabel(r'Winkel $\\phi \\, [\\mathrm{rad}]$')\n",
"axes[0].legend(loc='lower left')\n",
"axes[1].legend(loc='lower left')"
"plt.plot(x, np.sin(x), label=r'$\\sin(\\phi)$')\n",
"plt.xlim(0, 2 * np.pi)\n",
"plt.legend(loc='lower left')\n",
"\n",
"# Erstelle eine zweite Achse im gleichen Gitter, aber an 2. Position\n",
"ax_lower = plt.subplot(212, sharex=ax_upper, sharey=ax_upper) # Verwende die gleichen x- und y-Limits wie oben\n",
"\n",
"# Funktionen plotten nun in die zweite Achse:\n",
"plt.plot(x, np.cos(x), label=r'$\\cos(\\phi)$')\n",
"plt.xlabel(r'Winkel $\\phi \\, [\\mathrm{rad}]$')\n",
"plt.legend(loc='lower left')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **Hinweis:** Matplotlib arbeitet mit _Abbildungen_ und _Achsen_. Wenn wir solche nicht explizit erstellen, wie im Beispiel oben, werden sie beim Aufrufen von Funktionen wie `plt.plot` automatisch erstellt. Du kannst sie auch jederzeit abrufen:\n",
">\n",
"> ```python\n",
"> fig = plt.gcf() # die aktuelle Abbildung\n",
"> ax = plt.gca() # die aktuelle Achse\n",
"> ```\n",
"> \n",
"> Wenn du genauer bestimmen willst, auf welche Achse du plotten möchtest, kannst du sie in Funktionsaufrufen auch angeben oder die Achse direkt verwenden:\n",
">\n",
"> ```python\n",
"> # Explizit auf Achse `ax` plotten:\n",
"> plt.plot(x, y, axis=ax) # oder:\n",
"> ax.plot(x, y)\n",
"> ```"
]
},
{
Expand Down Expand Up @@ -503,7 +531,7 @@
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
"collapsed": false
},
"source": [
"Sieht dein Plot etwa so aus?\n",
Expand All @@ -515,7 +543,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
Expand Down

0 comments on commit b65946c

Please sign in to comment.