Materialien/plot.py
Alexander Hammer f8447d85af magnetika
2025-01-06 14:07:43 +01:00

304 lines
13 KiB
Python

import math
import numpy as np
import matplotlib.pyplot as plt
def plot_Hystereseschleife_und_so(path, plot_file_name, start=0.0, end=0.0, entmagnetisierungskurve=0, vergroessert=0,
mu_r=0, permeabilitaet=0):
mu_0 = 4 * math.pi * (10 ** -7)
data = np.loadtxt(path, delimiter='\t', converters={0: lambda s: s.replace(b',', b'.').decode(),
1: lambda s: s.replace(b',', b'.').decode(),
2: lambda s: s.replace(b',', b'.').decode()})
if (start != 0 or end != 0) and mu_r == 0 and permeabilitaet == 0:
mask = (data[:, 0] >= start) & (data[:, 0] <= end)
flussdichte = data[mask, 2] * 0.674
feldstaerke = data[mask, 1] * 4187.5
elif permeabilitaet == 1:
flussdichte = data[:, 2]
feldstaerke = data[:, 1] * 4187.5
elif mu_r == 1:
skalierung = (528 * 10**-3) / ((730 + 749 + 732) * 731 * 225 * 10**-6 * 0.2 * mu_0)
print(skalierung)
flussdichte = abs(data[:, 2]) * skalierung
print(np.max(flussdichte))
feldstaerke = data[:, 1] * 4187.5
max_index = np.argmax(flussdichte)
print(feldstaerke[max_index])
else:
flussdichte = data[:, 2] * 0.674
feldstaerke = data[:, 1] * 4187.5
if mu_r == 0:
min_flussdichte = np.min(flussdichte)
max_flussdichte = np.max(flussdichte)
flussdichte_offset = (abs(min_flussdichte) + max_flussdichte) / 2
flussdichte = flussdichte + abs(min_flussdichte) - flussdichte_offset
plt.figure(figsize=(10, 7.5))
# plt.plot(feldstaerke, flussdichte, linestyle='', marker='.', label="Punkte", color="red")
if permeabilitaet == 0 and mu_r == 0:
plt.plot(feldstaerke, flussdichte, label="", color='cornflowerblue')
plt.xlabel(r'$H/\frac{A}{m}$', fontsize=12, labelpad=15, ha='center', va='center')
plt.ylabel(r'$B/T$', fontsize=12, rotation=0, labelpad=10, ha='center', va='center')
elif permeabilitaet == 1:
plt.plot(feldstaerke, -flussdichte, label="", color='cornflowerblue')
plt.xlabel(r'$H/\frac{A}{m}$', fontsize=12, labelpad=15, ha='center', va='center')
plt.ylabel(r"$u_y/V$", fontsize=12, rotation=0, labelpad=10, ha='center', va='center')
elif mu_r == 1:
plt.plot(feldstaerke, flussdichte, label="", color='cornflowerblue')
plt.xlabel(r'$H/\frac{A}{m}$', fontsize=12, labelpad=15, ha='center', va='center')
plt.ylabel(r"$\mu_r/1$", fontsize=12, rotation=0, labelpad=10, ha='center', va='center')
plt.grid()
plt.tight_layout()
if vergroessert == 1:
plt.ylim(-0.135, 0.135)
plt.xlim(-100, 100)
if entmagnetisierungskurve == 1:
plt.ylim(-1.5, 1.5)
plt.xlim(-1000, 1000)
ticks = np.arange(-1.5, 1.6, 0.5) # Ticks bei -1.5, -1.0, ..., 1.5
plt.yticks(ticks)
plt.savefig(plot_file_name, dpi=600, bbox_inches='tight')
plt.show()
def plot_test(path, plot_file_name):
data = np.loadtxt(path, delimiter='\t', converters={0: lambda s: s.replace(b',', b'.').decode(),
1: lambda s: s.replace(b',', b'.').decode(),
2: lambda s: s.replace(b',', b'.').decode()})
flussdichte = data[:, 2] * 0.674
feldstaerke = data[:, 1] * 4187.5
min_flussdichte = np.min(flussdichte)
max_flussdichte = np.max(flussdichte)
flussdichte_offset = (abs(min_flussdichte) + max_flussdichte) / 2
print(flussdichte_offset)
flussdichte = flussdichte + abs(min_flussdichte) - flussdichte_offset
B_s_plus = np.max(flussdichte)
B_s_minus = np.min(flussdichte)
H_s_plus = np.max(feldstaerke)
H_s_minus = np.min(feldstaerke)
# Plot der Hystereseschleife
plt.figure(figsize=(10, 7.5))
plt.plot(feldstaerke, flussdichte, label="Hystereseschleife", color="cornflowerblue")
plt.axhline(0, color="black", linestyle="-", linewidth=0.8) # y-Achse
plt.axvline(0, color="black", linestyle="-", linewidth=0.8) # x-Achse
# Schnittpunkte berechnen (näherungsweise)
# Mit x=0, finde die y-Schnittpunkte
x_achse_schnitt = feldstaerke[np.isclose(flussdichte, 0, atol=0.05)]
y_achse_schnitt = flussdichte[np.isclose(feldstaerke, 0, atol=14.5)]
print("H_c", x_achse_schnitt)
print("B_r", y_achse_schnitt)
print("+B_max", B_s_plus)
print("-B_max", B_s_minus)
print("+H_max", H_s_plus)
print("-H_max", H_s_minus)
# Schnittpunkte plotten und beschriften
for x_schnitt in x_achse_schnitt:
if x_schnitt > 0:
plt.scatter(x_schnitt - 9.85499, 0, color="red", zorder=5)
plt.text(+120 + x_schnitt, 0.05, r"+$H_c$", color="red", fontsize=12, ha="center")
else:
plt.scatter(x_schnitt + 7.85499, 0, color="red", zorder=5)
plt.text(-100 + x_schnitt, 0.05, r"-$H_c$", color="red", fontsize=12, ha="center")
for y_schnitt in y_achse_schnitt:
if y_schnitt > 0:
plt.scatter(0, y_schnitt - 0.02667, color="blue", zorder=5)
plt.text(-220, y_schnitt + 0.05, r"+$B_r$", color="blue", fontsize=12, va="center")
else:
plt.scatter(0, y_schnitt - 0.01667, color="blue", zorder=5)
plt.text(50, y_schnitt - 0.1, r"-$B_r$", color="blue", fontsize=12, va="center")
plt.hlines(y=B_s_plus, xmin=-30, xmax=30, color="blue", linewidth=1)
plt.text(50, B_s_plus, r"+$B_{max}$", color="blue", fontsize=12, va="center")
plt.hlines(y=B_s_minus, xmin=-30, xmax=30, color="blue", linewidth=1)
plt.text(50, B_s_minus, r"-$B_{max}$", color="blue", fontsize=12, va="center")
plt.vlines(x=H_s_plus, ymin=-0.033, ymax=0.033, color="red", linewidth=1)
plt.text(H_s_plus-25, -0.12, r"+$H_{max}$", color="red", fontsize=12, ha="center")
plt.vlines(x=H_s_minus, ymin=-0.033, ymax=0.033, color="red", linewidth=1)
plt.text(H_s_minus, -0.12, r"-$H_{max}$", color="red", fontsize=12, ha="center")
# Zusätzliche Anpassungen
plt.xlabel(r'$H/\frac{A}{m}$', fontsize=12, labelpad=15, ha='center', va='center')
plt.ylabel(r'$B/T$', fontsize=12, rotation=0, labelpad=10, ha='center', va='center')
plt.grid(True)
plt.tight_layout()
plt.savefig(plot_file_name, dpi=600, bbox_inches='tight')
plt.show()
def plot_Strom(path, plot_file_name, start=0.0, end=0.0, strom=0):
data = np.loadtxt(path, delimiter='\t', converters={0: lambda s: s.replace(b',', b'.').decode(),
1: lambda s: s.replace(b',', b'.').decode(),
2: lambda s: s.replace(b',', b'.').decode()})
mask = (data[:, 0] >= start) & (data[:, 0] <= end)
strom = data[mask, 1]
strom_max = np.max(strom)
print(strom_max)
time = data[mask, 0]
y_diff = -data[mask, 2]
print(np.max(y_diff))
fig, ax1 = plt.subplots(figsize=(10, 7.5))
ax2 = ax1.twinx()
ax2.plot(time, strom, 'darkorange', label="Magnetisierungsstrom i")
ax2.set_ylabel(r"i/A", color='darkorange', fontsize=12, rotation=0, labelpad=10, ha='center', va='center') # Beschriftung der linken y-Achse
ax2.tick_params(axis='y', labelcolor="darkorange")
# Erstelle die zweite y-Achse, die dieselbe x-Achse verwendet
ax1.plot(time, y_diff, 'cornflowerblue', label=r"Induzierte Spannung $u_y$")
ax1.set_ylabel(r"$u_y/V$", color='cornflowerblue', fontsize=12, rotation=0, labelpad=10, ha='center', va='center') # Beschriftung der rechten y-Achse
ax1.tick_params(axis='y', labelcolor="cornflowerblue")
ax1.set_yticks(np.arange(-0.5, 0.6, 0.1))
ax2.set_yticks(np.arange(-0.5, 0.6, 0.1))
ax2.set_ylim(-0.55, 0.55) # Setzt den Bereich für Strom
ax1.set_ylim(-0.55, 0.55) # Setzt den Bereich für Strom
ax1.grid(True, which='both', axis='both', linestyle='-', linewidth=0.8)
# ax2.grid(True, which='both', axis='y', linestyle='-', linewidth=0.8)
# if strom == 1:
# if start != 0 or end != 0:
# mask = (data[:, 0] >= start) & (data[:, 0] <= end)
# strom = data[mask, 1]
# time = data[mask, 0]
# else:
# strom = data[:, 1]
# time = data[:, 0]
# plt.figure(figsize=(10, 7.5))
# plt.plot(time, strom, label="", color='cornflowerblue')
# else:
# if start != 0 or end != 0:
# mask = (data[:, 0] >= start) & (data[:, 0] <= end)
# y_diff = -data[mask, 2]
# time = data[mask, 0]
# else:
# y_diff = -data[:, 2]
# time = data[:, 0]
# plt.figure(figsize=(10, 7.5))
# plt.plot(time, y_diff, label="", color='cornflowerblue')
ax1.set_xlabel(r'$t/s$', fontsize=12, labelpad=15, ha='center', va='center')
# plt.ylabel(r'$I/A$', fontsize=12, rotation=0, labelpad=10, ha='center', va='center')
plt.tight_layout()
ax1.legend(loc="upper right") # Legende für die Spannung auf der linken Seite
ax2.legend(loc="lower left")
plt.savefig(plot_file_name, dpi=600, bbox_inches='tight')
plt.show()
def plot_Neukurve(path, plot_file_name, start=0.0, end=0.0):
data = np.loadtxt(path, delimiter='\t', converters={0: lambda s: s.replace(b',', b'.').decode(),
1: lambda s: s.replace(b',', b'.').decode(),
2: lambda s: s.replace(b',', b'.').decode()})
mask = (data[:, 0] >= start) & (data[:, 0] <= end)
flussdichte = data[mask, 2] * 0.674
feldstaerke = data[mask, 1] * 4187.5
plt.figure(figsize=(10, 7.5))
# Schwellenwert definieren
y_schwelle = 1.1728 # Ersetze mit deinem Schwellenwert
# Punkt finden, an dem y_schwelle das erste Mal überschritten wird
ueberschritten = flussdichte >= y_schwelle
wechsel_index = ueberschritten.argmax() if ueberschritten.any() else len(flussdichte)
# Daten in zwei Abschnitte teilen
x1, y1 = feldstaerke[:wechsel_index + 1], flussdichte[:wechsel_index + 1]
x2, y2 = feldstaerke[wechsel_index:], flussdichte[wechsel_index:]
# Plot erstellen
plt.figure(figsize=(10, 7.5))
plt.plot(x1, y1, color='darkorange', label='Bis Schwellenwert', zorder=5)
plt.plot(x2, y2, color='cornflowerblue', label='Nach Schwellenwert')
plt.xlabel(r'$H/\frac{A}{m}$', fontsize=12, labelpad=15, ha='center', va='center')
plt.ylabel(r"$B/T$", fontsize=12, rotation=0, labelpad=10, ha='center', va='center')
plt.grid()
plt.tight_layout()
plt.savefig(plot_file_name, dpi=600, bbox_inches='tight')
plt.show()
def plot_Messung_Magnetfeld(plot_file_name):
# Die Daten als numpy Arrays
mu_0 = 4 * math.pi * (10 ** -7)
stromstaerke = np.array([1, 2, 3, 4, 6, 8, 10, 12.5, 15, 17.5, 20, 22.5, 25, 27.5, 30])
flussdichte_mT = np.array([160, 300, 470, 648, 936, 1233, 1430, 1580, 1666, 1735, 1794, 1838, 1855, 1898, 1920])
print(len(stromstaerke))
windungen = np.array([])
for element1, element2 in zip(stromstaerke, flussdichte_mT):
durchflutung = ((element2 * 10**-3) / mu_0) * 14 * 10**-3
windungszahl = durchflutung / element1
windungen = np.append(windungen, windungszahl)
print(windungen)
print(np.sum(windungen) / len(windungen))
# Umwandlung der Flussdichte von milliTesla zu Tesla (1 mT = 0.001 T)
# flussdichte_T = flussdichte_mT / 1000
# Erstellen des Plots
plt.figure(figsize=(8, 6))
plt.plot(stromstaerke, flussdichte_mT, marker='o', color='cornflowerblue', label='Flussdichte B')
# Achsenbeschriftungen und Titel
plt.xlabel('I/A', fontsize=12, labelpad=15, ha='center', va='center')
plt.ylabel('B/mT', fontsize=12, rotation=0, labelpad=20, ha='center', va='bottom')
# plt.title('Stromstärke vs. Magnetische Flussdichte')
# Gitter und Legende hinzufügen
plt.grid(True)
plt.tight_layout()
plt.legend()
# Plot anzeigen
plt.savefig(plot_file_name, dpi=600, bbox_inches='tight')
plt.show()
# plot_Hystereseschleife_und_so('18112024-1543.dat', "1543-Hystereseschleife_100mHz.png")
# plot_test('18112024-1543.dat', "1543-Hystereseschleife_100mHz_Kennwerte.png")
# plot_Hystereseschleife_und_so('18112024-1545.dat', "1545-f-abhängige Hystereseschleife.png", end=26.7)
# plot_Hystereseschleife_und_so('18112024-1620.dat', "1620-Permeabilität_diff.png", permeabilitaet=1)
# plot_Hystereseschleife_und_so('18112024-1620.dat', "1620-Permeabilität_diff.png", mu_r=1)
# plot_Hystereseschleife_und_so('18112024-1620.dat', "1620-Permeabilität_r.png", mu_r=1)
# plot_Hystereseschleife_und_so('18112024-1636.dat', '1636-Entmagnetisierungskurve.png', entmagnetisierungskurve=1)
# plot_Hystereseschleife_und_so('18112024-1636.dat', '1636-Entmagnetisierungskurve_gross.png', vergroessert=1)
# plot_Neukurve('18112024-1639.dat', '1639-Neukurve.png', end=17.6)
# plot_Strom('18112024-1620.dat', "1620-strom.png", end=10, strom=1)
# plot_Strom('18112024-1620.dat', "1620-Permeabilität_diff_time.png", end=10)
plot_Messung_Magnetfeld("Messung_Magnetfeld.png")