diff --git a/202 - Plots mit Matplotlib.ipynb b/202 - Plots mit Matplotlib.ipynb index 32e42b2..809619a 100644 --- a/202 - Plots mit Matplotlib.ipynb +++ b/202 - Plots mit Matplotlib.ipynb @@ -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:" ] }, { @@ -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" ] }, { @@ -117,7 +158,7 @@ }, "outputs": [], "source": [ - "#plt.plot?" + "#axes1.plot?" ] }, { @@ -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)" ] }, { @@ -153,7 +197,7 @@ }, "outputs": [], "source": [ - "#plt.scatter?" + "#axes2.scatter?" ] }, { @@ -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))" ] }, { @@ -182,7 +229,7 @@ }, "outputs": [], "source": [ - "#plt.errorbar?" + "#axes3.errorbar?" ] }, { @@ -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)" ] }, { @@ -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." ] }, { @@ -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)" ] }, { @@ -248,7 +301,7 @@ }, "outputs": [], "source": [ - "#plt.imshow?" + "#axes5.imshow?" ] }, { @@ -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')" ] }, { @@ -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:" ] }, { @@ -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')" ] }, { @@ -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:" ] }, { @@ -348,7 +406,7 @@ }, "outputs": [], "source": [ - "plt.savefig('plots/my_plot.png')" + "abb7.savefig('plots/my_plot.png')" ] }, { @@ -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", "> ```" ] }, @@ -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()" ] }, { @@ -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()" ] }, { diff --git a/203 - Fits mit Scipy.ipynb b/203 - Fits mit Scipy.ipynb index 80561d4..1f05dc0 100644 --- a/203 - Fits mit Scipy.ipynb +++ b/203 - Fits mit Scipy.ipynb @@ -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]" ] }, @@ -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')" ] }, { @@ -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" ] }, diff --git "a/302 - Arbeiten mit Physikalischen Gr\303\266\303\237en.ipynb" "b/302 - Arbeiten mit Physikalischen Gr\303\266\303\237en.ipynb" index ff8007a..3e2eeb1 100644 --- "a/302 - Arbeiten mit Physikalischen Gr\303\266\303\237en.ipynb" +++ "b/302 - Arbeiten mit Physikalischen Gr\303\266\303\237en.ipynb" @@ -29,7 +29,7 @@ }, "outputs": [], "source": [ - "def plot_data(xdata, ydata, xerr=None, yerr=None, ax=plt, **kwargs):\n", + "def plot_data(ax, xdata, ydata, xerr=None, yerr=None, **kwargs):\n", " # retrieve errors from data\n", " xdata, xerr = unp.nominal_values(xdata), unp.std_devs(xdata)\n", " if (xerr == 0).all():\n", @@ -54,7 +54,7 @@ }, "outputs": [], "source": [ - "plot_data(x, y)" + "plot_data(fig1axes, x, y)" ] }, { @@ -118,9 +118,12 @@ "t = np.linspace(0, 10, 100) * units.seconds\n", "a = -9.81 * units.meter / units.seconds**2\n", "s = a / 2 * t**2\n", - "plt.plot(t, s)\n", - "plt.xlabel(\"Zeit $t \\, [{:~L}]$\".format(t.units))\n", - "plt.ylabel(\"Position $s \\, [{:~L}]$\".format(s.units))" + "\n", + "fig1 = plt.figure()\n", + "axes1 = fig1.add_subplot(111)\n", + "axes1.plot(t, s)\n", + "axes1.set_xlabel(\"Zeit $t \\, [{:~L}]$\".format(t.units))\n", + "axes1.set_ylabel(\"Position $s \\, [{:~L}]$\".format(s.units))" ] }, { @@ -131,8 +134,8 @@ }, "outputs": [], "source": [ - "plot_data(date, T)\n", - "plt.ylabel('Temperatur $T [{:~L}]$'.format(T.units))" + "plot_data(axes1, date, T)\n", + "axes1.set_ylabel('Temperatur $T [{:~L}]$'.format(T.units))" ] }, { @@ -190,9 +193,9 @@ }, "outputs": [], "source": [ - "def plot_labels(xdata, ydata):\n", - " plt.xlabel(\"${:~P}$\".format(xdata.units))\n", - " plt.ylabel(\"${:~P}$\".format(ydata.units))" + "def plot_labels(ax, xdata, ydata):\n", + " ax.xlabel(\"${:~P}$\".format(xdata.units))\n", + " ax.ylabel(\"${:~P}$\".format(ydata.units))" ] }, { @@ -204,7 +207,7 @@ "outputs": [], "source": [ "plot_data(u.magnitude, v.magnitude)\n", - "plot_labels(u, v)" + "plot_labels(axes2, u, v)" ] }, { @@ -255,7 +258,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.5.2" } }, "nbformat": 4,