Skip to content

Commit

Permalink
Merge pull request #332 from pep-dortmund/326-matplotlib-object-orien…
Browse files Browse the repository at this point in the history
…ted-approach

326 matplotlib object oriented approach
  • Loading branch information
maxnoe committed Jun 15, 2023
2 parents ad34117 + 4c4ebe0 commit 7969828
Show file tree
Hide file tree
Showing 37 changed files with 623 additions and 474 deletions.
16 changes: 9 additions & 7 deletions exercises-latex/12-python/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ def f(t, a, b, c, d):

t_plot = np.linspace(-0.5, 2 * np.pi + 0.5, 1000) * 1e-3

plt.errorbar(
fig, ax = plt.subplots(1, 1, layout="constrained")

ax.errorbar(
t * 1e3,
noms(U) * 1e-3,
yerr=stds(U) * 1e-3,
fmt="k_",
label="Daten",
)
plt.plot(t_plot * 1e3, f(t_plot, *noms(params)) * 1e-3, "-", label="Fit")
plt.xlim(t_plot[0] * 1e3, t_plot[-1] * 1e3)
plt.xlabel(r"$t \mathbin{/} \unit{\milli\second}$")
plt.ylabel(r"$U \mathbin{/} \unit{\kilo\volt}$")
plt.legend()
plt.savefig("build/loesung-plot.pdf")
ax.plot(t_plot * 1e3, f(t_plot, *noms(params)) * 1e-3, "-", label="Fit")
ax.set_xlim(t_plot[0] * 1e3, t_plot[-1] * 1e3)
ax.set_xlabel(r"$t \mathbin{/} \unit{\milli\second}$")
ax.set_ylabel(r"$U \mathbin{/} \unit{\kilo\volt}$")
ax.legend(loc="best")
fig.savefig("build/loesung-plot.pdf")

t1, t2 = np.array_split(t * 1e3, 2)
U1, U2 = np.array_split(U * 1e-3, 2)
Expand Down
9 changes: 5 additions & 4 deletions exercises-toolbox/2-numpy/2-indexing/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
print("Spielfeld zu Beginn des Spiels:")
print(field)

mat = plt.matshow(field, cmap="Set1", vmax=5, vmin=-1)
plt.savefig("Spielfeld_Beginn.pdf")
fig, ax = plt.subplots(1, 1, layout="constrained")
mat = ax.matshow(field, cmap="Set1", vmax=5, vmin=-1)
fig.savefig("Spielfeld_Beginn.pdf")

# Lösung für Aufgabe 1:

Expand Down Expand Up @@ -54,5 +55,5 @@
print("Spielfeld zum Ende des Spiels:")
print(field)

mat = plt.matshow(field, cmap="Set1", vmax=5, vmin=-1)
plt.savefig("Spielfeld_Ende.pdf")
mat.set_array(field)
fig.savefig("Spielfeld_Ende.pdf")
9 changes: 5 additions & 4 deletions exercises-toolbox/2-numpy/2-indexing/vorlage.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

# Was das hier ist lernen wir noch, kann getrost ignoriert werden.
# In der Zwischenzeit, die Kurzfassung: Das 'macht' die Bilder. ;-)
plt.matshow(field, cmap="Set1", vmax=5, vmin=-1)
plt.savefig("Spielfeld_Beginn.pdf")
fig, ax = plt.subplots(1, 1, layout="constrained")
mat = ax.matshow(field, cmap="Set1", vmax=5, vmin=-1)
fig.savefig("Spielfeld_Beginn.pdf")

# Hier die Lösung für Aufgabe 1 und 2 eintragen (ein Beispiel ist angegeben):
# Durch Ausführen dieser Datei kann Schritt für Schritt der Fortschritt
Expand All @@ -38,5 +39,5 @@

# Was das hier ist lernen wir noch, kann getrost ignoriert werden.
# In der Zwischenzeit, die Kurzfassung: Das 'macht' die Bilder. ;-)
plt.matshow(field, cmap="Set1", vmax=5, vmin=-1)
plt.savefig("Spielfeld_Ende.pdf")
mat.set_array(field)
fig.savefig("Spielfeld_Ende.pdf")
10 changes: 5 additions & 5 deletions exercises-toolbox/3-matplotlib/1/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

x = np.linspace(0, 1)

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

plt.plot(x, x**2)
ax.plot(x, x**2)

plt.xlabel(r"$x$")
plt.ylabel(r"$x^2$")
ax.set_xlabel(r"$x$")
ax.set_ylabel(r"$x^2$")

plt.savefig("loesung.pdf")
fig.savefig("loesung.pdf")
12 changes: 6 additions & 6 deletions exercises-toolbox/3-matplotlib/2/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

x = np.linspace(0, 1)

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

plt.plot(x, x**2, label=r"$x^2$")
plt.plot(x, x**5, "x", label=r"$x^5$")
ax.plot(x, x**2, label=r"$x^2$")
ax.plot(x, x**5, "x", label=r"$x^5$")

plt.legend(loc="best")
plt.xlabel(r"$x$")
ax.legend(loc="best")
ax.set_xlabel(r"$x$")

plt.savefig("loesung.pdf")
fig.savefig("loesung.pdf")
20 changes: 9 additions & 11 deletions exercises-toolbox/3-matplotlib/3/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@

x, y = np.genfromtxt("3.txt", unpack=True)

plt.figure(layout="constrained")
fig, (ax1, ax2) = plt.subplots(1, 2, layout="constrained")

plt.subplot(1, 2, 1)
plt.plot(x, y, ".")
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
ax1.plot(x, y, ".")
ax1.set_xlabel(r"$x$")
ax1.set_ylabel(r"$y$")

plt.subplot(1, 2, 2)
plt.plot(x, y, ".")
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
plt.yscale("log")
ax2.plot(x, y, ".")
ax2.set_xlabel(r"$x$")
ax2.set_ylabel(r"$y$")
ax2.set_yscale("log")

plt.savefig("loesung.pdf")
fig.savefig("loesung.pdf")
16 changes: 8 additions & 8 deletions exercises-toolbox/3-matplotlib/4/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

t = np.linspace(-0.5, 2 * np.pi + 0.5)

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

plt.errorbar(x, y, xerr=e_x, yerr=e_y, fmt="k.", label="Daten")
plt.plot(t, np.sin(t), label=r"$\sin(t)$")
ax.errorbar(x, y, xerr=e_x, yerr=e_y, fmt="k.", label="Daten")
ax.plot(t, np.sin(t), label=r"$\sin(t)$")

plt.xlim(t[0], t[-1])
plt.xlabel(r"$t$")
plt.ylabel(r"$f(t)$")
ax.set_xlim(t[0], t[-1])
ax.set_xlabel(r"$t$")
ax.set_ylabel(r"$f(t)$")

plt.legend()
plt.savefig("loesung.pdf")
ax.legend()
fig.savefig("loesung.pdf")
18 changes: 9 additions & 9 deletions exercises-toolbox/3-matplotlib/5/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

x = np.linspace(0, 2 * np.pi)

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

plt.plot(x, np.sin(x), label=r"$\sin(x)$")
plt.plot(x, np.cos(x), label=r"$\cos(x)$")
ax.plot(x, np.sin(x), label=r"$\sin(x)$")
ax.plot(x, np.cos(x), label=r"$\cos(x)$")

plt.xlim(0, 2 * np.pi)
plt.ylim(-1.2, 1.2)
plt.xlabel(r"$x$")
ax.set_xlim(0, 2 * np.pi)
ax.set_ylim(-1.2, 1.2)
ax.set_xlabel(r"$x$")

plt.xticks(
ax.set_xticks(
np.arange(0, 2.1 * np.pi, np.pi / 2),
[
r"$0$",
Expand All @@ -23,5 +23,5 @@
],
)

plt.legend()
plt.savefig("loesung.pdf")
ax.legend()
fig.savefig("loesung.pdf")
10 changes: 5 additions & 5 deletions exercises-toolbox/3-matplotlib/6/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

x = np.linspace(0, 1, 100)

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

for n in range(1, 11):
plt.plot(x, x**n, label=f"$x^{{{n}}}$")
ax.plot(x, x**n, label=f"$x^{{{n}}}$")

plt.legend(loc="upper left")
plt.xlabel("$x$")
plt.savefig("loesung.pdf")
ax.legend(loc="upper left")
ax.set_xlabel("$x$")
fig.savefig("loesung.pdf")
14 changes: 7 additions & 7 deletions exercises-toolbox/3-matplotlib/7/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

x = np.linspace(0, 2 * np.pi, 100)

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

for A in (-1, -0.5, 0.5, 1):
plt.plot(x, A * np.cos(x), label=f"$A = {A}$")
ax.plot(x, A * np.cos(x), label=f"$A = {A}$")

plt.legend()
plt.xlim(0, 2 * np.pi)
plt.xlabel("$x$")
plt.ylabel(r"$A \cos(x)$")
plt.savefig("loesung.pdf")
ax.legend()
ax.set_xlim(0, 2 * np.pi)
ax.set_xlabel("$x$")
ax.set_ylabel(r"$A \cos(x)$")
fig.savefig("loesung.pdf")
18 changes: 9 additions & 9 deletions exercises-toolbox/4-scipy/3-curve_fit/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ def f(x, a, b, c, d):
parameters, pcov = curve_fit(f, x, y, sigma=e_y)
print(parameters, np.sqrt(np.diag(pcov)), sep="\n")

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

plt.errorbar(x, y, yerr=e_y, fmt="k.", label="Daten")
ax.errorbar(x, y, yerr=e_y, fmt="k.", label="Daten")

t = np.linspace(-0.5, 2 * np.pi + 0.5, 500)
plt.plot(t, f(t, *parameters), label="Fit")
plt.plot(t, np.sin(t), "--", label="Original")
ax.plot(t, f(t, *parameters), label="Fit")
ax.plot(t, np.sin(t), "--", label="Original")

plt.xlim(t[0], t[-1])
plt.xlabel(r"$t$")
plt.ylabel(r"$f(t)$")
plt.legend()
ax.set_xlim(t[0], t[-1])
ax.set_xlabel(r"$t$")
ax.set_ylabel(r"$f(t)$")
ax.legend()

plt.savefig("loesung.pdf")
fig.savefig("loesung.pdf")
20 changes: 11 additions & 9 deletions exercises-toolbox/4-scipy/3-curve_fit/vorlage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ def f(x, a, b, c, d):
parameters = ?

# Dieser Code erstellt einen Plot mithilfe von f und parameters
plt.figure(layout="constrained")
plt.errorbar(x, y, yerr=e_y, fmt='rx', label='Daten')
fig, ax = plt.subplots(1, 1, layout="constrained")
ax.errorbar(x, y, yerr=e_y, fmt="rx", label="Daten")

t = np.linspace(-0.5, 2 * np.pi + 0.5)
plt.plot(t, f(t, *parameters), 'b-', label='Fit')
plt.plot(t, np.sin(t), 'g--', label='Original')
plt.xlim(t[0], t[-1])
plt.xlabel(r'$t$')
plt.ylabel(r'$f(t)$')
plt.legend()
plt.savefig('loesung.pdf')

ax.plot(t, f(t, *parameters), "b-", label="Fit")
ax.plot(t, np.sin(t), "g--", label="Original")
ax.set_xlim(t[0], t[-1])
ax.set_xlabel(r"$t$")
ax.set_ylabel(r"$f(t)$")
ax.legend(loc="best")
fig.savefig("loesung.pdf")
26 changes: 13 additions & 13 deletions exercises-toolbox/4-scipy/4-peakdetect/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ def e(x, a, b, c):
maxs, properties = find_peaks(U, prominence=1, distance=100)
mins, properties = find_peaks(-U, prominence=1, distance=100)

x = np.linspace(0, plt.xlim()[1], 500)

parameters_max, pcov_max = curve_fit(e, t[maxs], U[maxs])
print(parameters_max, np.sqrt(np.diag(pcov_max)), sep="\n")

parameters_min, pcov_min = curve_fit(e, t[mins], U[mins])
print(parameters_min, np.sqrt(np.diag(pcov_min)), sep="\n")

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

x = np.linspace(0, ax.get_xlim()[1], 500)

plt.plot(t, U, "k-", label="Gedämpfte Schwingung")
ax.plot(t, U, "k-", label="Gedämpfte Schwingung")

plt.plot(x, e(x, *parameters_max), label="Obere Einhüllende")
plt.plot(t[maxs], U[maxs], "o", color="C0", label="Maxima")
ax.plot(x, e(x, *parameters_max), label="Obere Einhüllende")
ax.plot(t[maxs], U[maxs], "o", color="C0", label="Maxima")

plt.plot(x, e(x, *parameters_min), label="Untere Einhüllende")
plt.plot(t[mins], U[mins], "o", color="C1", label="Minima")
ax.plot(x, e(x, *parameters_min), label="Untere Einhüllende")
ax.plot(t[mins], U[mins], "o", color="C1", label="Minima")

plt.xlabel(r"$t \ / \ \mathrm{ms}$")
plt.ylabel(r"$U \ / \ \mathrm{V}$")
plt.legend()
plt.xlim(0, 0.3)
plt.savefig("loesung.pdf")
ax.set_xlabel(r"$t \ / \ \mathrm{ms}$")
ax.set_ylabel(r"$U \ / \ \mathrm{V}$")
ax.set_xlim(0, 0.3)
ax.legend(loc="best")
fig.savefig("loesung.pdf")
22 changes: 11 additions & 11 deletions exercises-toolbox/4-scipy/4-peakdetect/vorlage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
parameters_min = #

# Dieser Code erzeugt den Plot
plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

plt.plot(t, U, 'b-', label='Gedämpfte Schwingung')
ax.plot(t, U, "b-", label="Gedämpfte Schwingung")

plt.plot(t[maxs], U[maxs], 'rx', label='Extrema')
plt.plot(x, e(x, *parameters_max), 'g-', label='Obere Einhüllende')
ax.plot(t[maxs], U[maxs], "rx", label="Extrema")
ax.plot(x, e(x, *parameters_max), "g-", label="Obere Einhüllende")

plt.plot(t[mins], U[mins], 'rx')
plt.plot(x, e(x, *parameters_min), 'y-', label='Untere Einhüllende')
ax.plot(t[mins], U[mins], "rx")
ax.plot(x, e(x, *parameters_min), "y-", label="Untere Einhüllende")

plt.xlabel(r'$t \ / \ \mathrm{ms}$')
plt.ylabel(r'$U \ / \ \mathrm{V}$')
plt.legend()
plt.xlim(0, 0.3)
plt.savefig('loesung.pdf')
ax.set_xlabel(r"$t \ / \ \mathrm{ms}$")
ax.set_ylabel(r"$U \ / \ \mathrm{V}$")
ax.set_xlim(0, 0.3)
ax.legend(loc="best")
fig.savefig("loesung.pdf")
14 changes: 7 additions & 7 deletions exercises-toolbox/4-scipy/5-beugung/loesung.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def theory(phi, A0, b):

x = np.linspace(-0.03, 0.03, 100)

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

plt.plot(x, theory(x, *parameters), "-", label="Fit")
plt.plot(phi, I, "k.", label="Daten")
ax.plot(x, theory(x, *parameters), "-", label="Fit")
ax.plot(phi, I, "k.", label="Daten")

plt.xlabel(r"$\varphi \ / \ \mathrm{rad}$")
plt.ylabel(r"$I \ / \ \mathrm{A}$")
plt.legend()
ax.set_xlabel(r"$\varphi \ / \ \mathrm{rad}$")
ax.set_ylabel(r"$I \ / \ \mathrm{A}$")
ax.legend(loc="best")

plt.savefig("loesung.pdf")
fig.savefig("loesung.pdf")
16 changes: 8 additions & 8 deletions exercises-toolbox/4-scipy/5-beugung/vorlage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def theory(phi, A0, b):
'''Hier Funktion ergänzen'''
"""Hier Funktion ergänzen"""


# Hier Fit ergänzen
Expand All @@ -13,13 +13,13 @@ def theory(phi, A0, b):
# Hier wird der Plot erstellt
x = np.linspace(-0.03, 0.03, 100)

plt.figure(layout="constrained")
fig, ax = plt.subplots(1, 1, layout="constrained")

plt.plot(x, theory(x, *parameters), '-', label='Fit')
plt.plot(phi, I, 'k.', label='Daten')
ax.plot(x, theory(x, *parameters), "-", label="Fit")
ax.plot(phi, I, "k.", label="Daten")

plt.xlabel(r'$\varphi \ / \ \mathrm{rad}$')
plt.ylabel(r'$I \ / \ \mathrm{A}$')
plt.legend()
ax.set_xlabel(r"$\varphi \ / \ \mathrm{rad}$")
ax.set_ylabel(r"$I \ / \ \mathrm{A}$")
ax.legend(loc="best")

plt.savefig('loesung.pdf')
fig.savefig("loesung.pdf")
Loading

0 comments on commit 7969828

Please sign in to comment.