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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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