Skip to content

Teach the object oriented matplotlib API instead of the procedural one. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 111 additions & 48 deletions 202 - Plots mit Matplotlib.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@
"source": [
"## Einfaches Plotten\n",
"\n",
"PyPlot stellt einige grundlegende Funktionen zur Verfügung, mit denen wir Daten schnell plotten können:"
"PyPlot besitzt ein einfaches prozedurales Interface, mit denen wir mit nur einem Befehl Daten plotten können. Da dieses jedoch schnell an seine Grenzen kommt, wollen hier jedoch gleich das später unumgängliche, \"schönere\", objekt-orientierte Interface benutzen. Wer nun ein besonders schwieriges Kapitel fürchtet, der sei beruhigt, die zusätzliche Komplexität des objekt-orientierten Interface beschränkt sich für uns darauf, dass wir vor jedem Plot zwei weitere Befehle absetzen müssen:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- **`plot`** nimmt x- und y-Daten sowie eine Vielzahl von optionalen Argumenten zur Konfiguration an. Fehlen die x-Daten, werden die Indizes der y-Daten verwendet:"
"- Um einen Plot zu erstellen brauchen wir zunächst zwei Objekte, die wir erstellen müssen. Eines repräsentiert die Abbildung, in das unser Plot gezeichnet werden wird, das andere repräsentiert einen Axenplot:"
]
},
{
Expand All @@ -99,7 +99,48 @@
},
"outputs": [],
"source": [
"plt.plot(np.arange(100)**2)"
"abb1 = plt.figure()\n",
"axes1 = abb1.add_subplot(1,1,1) # Für's erste "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unser plot-Objekt besitzt eine Vielzahl an Methoden, mit denen wir unseren Plot füllen und anpassen können\n",
"- **`plot`** füllt unseren Plot mit einer Kurve. Die Methode nimmt x- und y-Daten sowie eine Vielzahl von optionalen Argumenten zur Konfiguration an. Fehlen die x-Daten, werden die Indizes der y-Daten verwendet:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"axes1.plot(np.arange(100)**2)\n",
"\n",
"abb1 # weist Jupyter an, das Bild abb1 im Notebook anzuzeigen"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ein weitere Kurve kann einfach durch einen weiteren Aufruf von **`plot`** zu unserem bestehenden Plot hinzugefügt werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"axes1.plot(100 * np.arange(100))\n",
"abb1"
]
},
{
Expand All @@ -117,7 +158,7 @@
},
"outputs": [],
"source": [
"#plt.plot?"
"#axes1.plot?"
]
},
{
Expand All @@ -142,7 +183,10 @@
},
"outputs": [],
"source": [
"plt.scatter(np.arange(10), np.arange(10)**2)"
"abb2 = plt.figure()\n",
"axes2 = abb2.add_subplot(1,1,1)\n",
"\n",
"axes2.scatter(np.arange(10), np.arange(10)**2)"
]
},
{
Expand All @@ -153,7 +197,7 @@
},
"outputs": [],
"source": [
"#plt.scatter?"
"#axes2.scatter?"
]
},
{
Expand All @@ -171,7 +215,10 @@
},
"outputs": [],
"source": [
"plt.errorbar(np.arange(10), np.arange(10)**2, yerr=np.arange(10))"
"abb3 = plt.figure()\n",
"axes3 = abb3.add_subplot(1,1,1)\n",
"\n",
"axes3.errorbar(np.arange(10), np.arange(10)**2, yerr=np.arange(10))"
]
},
{
Expand All @@ -182,7 +229,7 @@
},
"outputs": [],
"source": [
"#plt.errorbar?"
"#axes3.errorbar?"
]
},
{
Expand All @@ -200,7 +247,10 @@
},
"outputs": [],
"source": [
"_ = plt.hist(np.random.normal(size=100), bins=10)"
"abb4 = plt.figure()\n",
"axes4 = abb4.add_subplot(1,1,1)\n",
"\n",
"_ = axes4.hist(np.random.normal(size=100), bins=10)"
]
},
{
Expand All @@ -211,14 +261,14 @@
},
"outputs": [],
"source": [
"#plt.hist?"
"#axes4.hist?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **Hinweis:** Alternativ könnt ihr Histogramme mit `numpy.histogram` und `matplotlib.pyplot.bar` plotten. Diese Methode bietet etwas mehr Kontrolle über die Berechnung der Histogrammdaten."
"> **Hinweis:** Alternativ könnt ihr Histogramme mit `numpy.histogram` und `matplotlib.axes.Axes.bar` plotten. Diese Methode bietet etwas mehr Kontrolle über die Berechnung der Histogrammdaten."
]
},
{
Expand All @@ -236,8 +286,11 @@
},
"outputs": [],
"source": [
"plt.imshow(np.random.random((64, 64)), interpolation='none')\n",
"plt.colorbar()"
"abb5 = plt.figure()\n",
"axes5 = abb5.add_subplot(1,1,1)\n",
"\n",
"im5 = axes5.imshow(np.random.random((64, 64)), interpolation='none')\n",
"abb5.colorbar(im5)"
]
},
{
Expand All @@ -248,7 +301,7 @@
},
"outputs": [],
"source": [
"#plt.imshow?"
"#axes5.imshow?"
]
},
{
Expand Down Expand Up @@ -284,20 +337,23 @@
},
"outputs": [],
"source": [
"abb6 = plt.figure()\n",
"axes6 = abb6.add_subplot(1,1,1)\n",
"\n",
"x = np.linspace(0, 2 * np.pi, 100)\n",
"# Plot mit Label für Legende\n",
"plt.plot(x, np.sin(x), label=r'$A \\times \\sin(\\phi)$')\n",
"plt.plot(x, np.cos(x), label=r'$A \\times \\cos(\\phi)$')\n",
"axes6.plot(x, np.sin(x), label=r'$A \\times \\sin(\\phi)$')\n",
"axes6.plot(x, np.cos(x), label=r'$A \\times \\cos(\\phi)$')\n",
"# Titel\n",
"plt.title('Oszillation')\n",
"axes6.set_title('Oszillation')\n",
"# Achsenlimits\n",
"plt.xlim(0, 2 * np.pi)\n",
"plt.ylim(-1, 1)\n",
"axes6.set_xlim(0, 2 * np.pi)\n",
"axes6.set_ylim(-1, 1)\n",
"# Achsenbeschriftungen\n",
"plt.xlabel(r'Winkel $\\phi \\, [\\mathrm{rad}]$')\n",
"plt.ylabel(r'Auslenkung $d \\, [\\mathrm{cm}]$')\n",
"axes6.set_xlabel(r'Winkel $\\phi \\, [\\mathrm{rad}]$')\n",
"axes6.set_ylabel(r'Auslenkung $d \\, [\\mathrm{cm}]$')\n",
"# Legende\n",
"plt.legend(loc='lower left')"
"axes6.legend(loc='lower left')"
]
},
{
Expand All @@ -306,7 +362,7 @@
"source": [
"## Mehrere Plots in einer Abbildung\n",
"\n",
"Eine Abbildung kann in mehrere `subplots` aufgeteilt werden:"
"Eine Abbildung kann ganz einfach in mehrere `subplots` aufgeteilt werden. Hier zeigt sich jetzt die Bedeutung der drei einsen, die wir als Parameter dem `add_subplot` Befehl übergeben haben:"
]
},
{
Expand All @@ -318,17 +374,19 @@
"outputs": [],
"source": [
"# Eine Abbildung mit 2x1 Subplots erstellen\n",
"fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=True)\n",
"abb7 = plt.figure()\n",
"axes7_1 = abb7.add_subplot(2,1,1) # Ersten von 2x1 Subplots erstellen\n",
"axes7_2 = abb7.add_subplot(2,1,2) # Zweiten von 2x1 Subplots erstellen\n",
"# In beiden Subplots plotten\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",
"axes7_1.plot(x, np.sin(x), label=r'$\\sin(\\phi)$')\n",
"axes7_2.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')"
"abb7.suptitle('Oszillation')\n",
"axes7_1.set_xlim(0, 2 * np.pi)\n",
"axes7_2.set_xlabel(r'Winkel $\\phi \\, [\\mathrm{rad}]$')\n",
"axes7_1.legend(loc='lower left')\n",
"axes7_2.legend(loc='lower left')"
]
},
{
Expand All @@ -337,7 +395,7 @@
"source": [
"## Plots speichern\n",
"\n",
"Mit `matplotlib.pyplot.savefig` könnt ihr einen Plot als Bilddatei speichern:"
"Mit `matplotlib.figure.Figure.savefig` könnt ihr einen Plot als Bilddatei speichern:"
]
},
{
Expand All @@ -348,7 +406,7 @@
},
"outputs": [],
"source": [
"plt.savefig('plots/my_plot.png')"
"abb7.savefig('plots/my_plot.png')"
]
},
{
Expand All @@ -358,9 +416,8 @@
"> **Hinweis:** Um einen Plot im **DIN A4-Format** zu speichern könnt ihr dessen Größe und Auflösung anpassen:\n",
">\n",
"> ```python\n",
"> fig = plt.gcf()\n",
"> fig.set_size_inches(11.69, 8.27)\n",
"> plt.savefig(filename, dpi=150)\n",
"> abb7.set_size_inches(11.69, 8.27)\n",
"> abb7.savefig(filename, dpi=150)\n",
"> ```"
]
},
Expand Down Expand Up @@ -418,12 +475,15 @@
},
"outputs": [],
"source": [
"plt.plot(date, T, label='Messwerte')\n",
"plt.title('Temperaturverlauf in Heidelberg')\n",
"plt.xlim(np.min(date), np.max(date))\n",
"plt.xlabel(r'Zeitpunkt')\n",
"plt.ylabel(r'Temperatur $T \\, [^\\circ{}\\mathrm{C}]$')\n",
"plt.legend()"
"aufg1aAbb = plt.figure()\n",
"aufg1aAxes = aufg1aAbb.add_subplot(1,1,1)\n",
"\n",
"aufg1aAxes.plot(date, T, label='Messwerte')\n",
"aufg1aAxes.set_title('Temperaturverlauf in Heidelberg')\n",
"aufg1aAxes.set_xlim(np.min(date), np.max(date))\n",
"aufg1aAxes.set_xlabel(r'Zeitpunkt')\n",
"aufg1aAxes.set_ylabel(r'Temperatur $T \\, [^\\circ{}\\mathrm{C}]$')\n",
"aufg1aAxes.legend()"
]
},
{
Expand Down Expand Up @@ -492,12 +552,15 @@
},
"outputs": [],
"source": [
"plt.scatter(date % 1, T, marker='.', label='Messwerte')\n",
"plt.title('Jahres-Temperaturverlauf in Heidelberg')\n",
"plt.xlim(0, 1)\n",
"plt.xlabel(r'Zeitpunkt innerhalb des Jahres')\n",
"plt.ylabel(r'Temperatur $T \\, [^\\circ{}\\mathrm{C}]$')\n",
"plt.legend()"
"aufg1bAbb = plt.figure()\n",
"aufg1bAxes = aufg1bAbb.add_subplot(1,1,1)\n",
"\n",
"aufg1bAxes.scatter(date % 1, T, marker='.', label='Messwerte')\n",
"aufg1bAxes.set_title('Jahres-Temperaturverlauf in Heidelberg')\n",
"aufg1bAxes.set_xlim(0, 1)\n",
"aufg1bAxes.set_xlabel(r'Zeitpunkt innerhalb des Jahres')\n",
"aufg1bAxes.set_ylabel(r'Temperatur $T \\, [^\\circ{}\\mathrm{C}]$')\n",
"aufg1bAxes.legend()"
]
},
{
Expand Down
47 changes: 29 additions & 18 deletions 203 - Fits mit Scipy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,18 @@
"source": [
"# Zelle ausführen, um Beispieldaten zu generieren\n",
"samples = np.random.normal(loc=0, scale=1, size=1000)\n",
"N, bins, _ = plt.hist(samples, bins=50)\n",
"\n",
"abb1 = plt.figure()\n",
"abb1Axes = abb1.add_subplot(1, 1, 1)\n",
"\n",
"N, bins, _ = abb1Axes.hist(samples, bins=50)\n",
"x = bins[:-1] + (bins[1:] - bins[:-1]) / 2 # Mitte der Bins\n",
"dN = np.sqrt(N) # Fehler von N\n",
"plt.errorbar(x, N, dN, ls='none')\n",
"plt.xlabel(r'$x$')\n",
"plt.ylabel(r'$N$')\n",
"plt.title('Normalverteilte Beispieldaten')\n",
"\n",
"abb1Axes.errorbar(x, N, dN, ls='none')\n",
"abb1Axes.set_xlabel(r'$x$')\n",
"abb1Axes.set_ylabel(r'$N$')\n",
"abb1Axes.set_title('Normalverteilte Beispieldaten')\n",
"x, N, dN = x[N!=0], N[N!=0], dN[N!=0]"
]
},
Expand Down Expand Up @@ -241,14 +246,18 @@
},
"outputs": [],
"source": [
"# Abbildung und Axenplot generieren\n",
"abb2 = plt.figure()\n",
"abb2Axes = abb2.add_subplot(1, 1, 1)\n",
"\n",
"# Datenpunkte plotten\n",
"plt.errorbar(x, N, dN, ls='none', marker='.', color='grey', alpha=0.5, label='Messwerte')\n",
"abb2Axes.errorbar(x, N, dN, ls='none', marker='.', color='grey', alpha=0.5, label='Messwerte')\n",
"# Fit plotten\n",
"plt.plot(x, gaussian(x, *popt), label=\"\\n\".join([\"Gauß-Fit mit:\", r'$\\mu={:.3f}\\pm{:.3}$'.format(popt[0], np.sqrt(pcov[0][0])), r'$\\sigma={:.3f}\\pm{:.3f}$'.format(popt[1], np.sqrt(pcov[1][1]))]))\n",
"plt.legend()\n",
"plt.xlabel(r'$x$')\n",
"plt.ylabel(r'$N$')\n",
"plt.title('Gauß-Fit der Beispieldaten')"
"abb2Axes.plot(x, gaussian(x, *popt), label=\"\\n\".join([\"Gauß-Fit mit:\", r'$\\mu={:.3f}\\pm{:.3}$'.format(popt[0], np.sqrt(pcov[0][0])), r'$\\sigma={:.3f}\\pm{:.3f}$'.format(popt[1], np.sqrt(pcov[1][1]))]))\n",
"abb2Axes.legend()\n",
"abb2Axes.set_xlabel(r'$x$')\n",
"abb2Axes.set_ylabel(r'$N$')\n",
"abb2Axes.set_title('Gauß-Fit der Beispieldaten')"
]
},
{
Expand Down Expand Up @@ -404,15 +413,17 @@
},
"outputs": [],
"source": [
"plt.scatter(t, T, marker='.', color='gray', alpha=0.2, label='Messwerte')\n",
"a1abb = plt.figure()\n",
"a1axes = a1abb.add_subplot(1, 1, 1)\n",
"a1axes.scatter(t, T, marker='.', color='gray', alpha=0.2, label='Messwerte')\n",
"### BEGIN SOLUTION\n",
"t_model = np.linspace(0, 1, 100)\n",
"plt.plot(t_model, T_model(t_model, *popt), color='black', lw=2, label=\"\\n\".join([r'Fit $T(t)=T_0+T_A\\frac{-\\cos{\\!(2\\pi (t+\\Delta t))}+1}{2}$ mit:', r'$T_0={:.3f}\\pm{:.3f}'.format(popt[0], pcov[0][0]) + '^\\circ\\mathrm{C}$', r'$T_A={:.3f}\\pm{:.3f}'.format(popt[1], pcov[1][1]) + '^\\circ\\mathrm{C}$', r'$\\Delta t={:.3f}\\pm{:.3f}$'.format(popt[2], pcov[2][2])]))\n",
"plt.title('Jahres-Temperaturverlauf in Heidelberg')\n",
"plt.xlim(0, 1)\n",
"plt.xlabel(r'Zeitpunkt innerhalb des Jahres $t$')\n",
"plt.ylabel(r'Temperatur $T \\, [^\\circ{}\\mathrm{C}]$')\n",
"plt.legend(loc='lower center', fontsize='medium')\n",
"a1axes.plot(t_model, T_model(t_model, *popt), color='black', lw=2, label=\"\\n\".join([r'Fit $T(t)=T_0+T_A\\frac{-\\cos{\\!(2\\pi (t+\\Delta t))}+1}{2}$ mit:', r'$T_0={:.3f}\\pm{:.3f}'.format(popt[0], pcov[0][0]) + '^\\circ\\mathrm{C}$', r'$T_A={:.3f}\\pm{:.3f}'.format(popt[1], pcov[1][1]) + '^\\circ\\mathrm{C}$', r'$\\Delta t={:.3f}\\pm{:.3f}$'.format(popt[2], pcov[2][2])]))\n",
"a1axes.set_title('Jahres-Temperaturverlauf in Heidelberg')\n",
"a1axes.set_xlim(0, 1)\n",
"a1axes.set_xlabel(r'Zeitpunkt innerhalb des Jahres $t$')\n",
"a1axes.set_ylabel(r'Temperatur $T \\, [^\\circ{}\\mathrm{C}]$')\n",
"a1axes.legend(loc='lower center', fontsize='medium')\n",
"### END SOLUTION"
]
},
Expand Down
Loading