# Python Programm zum Plotten der berechneten Daten von Numerical_Differentiation_2.cpp
import matplotlib.pyplot as plt                                           # Python Bibliothek zum Plotten (siehe https://matplotlib.org/ )
from matplotlib import rcParams                                           # Globale Plot-Parameter von Matplotlib
import numpy as np                                                        # Python Bibliothek fuer Mathematisches (siehe https://numpy.org/ )
import matplotlib.gridspec as gridspec                                    # Mehrere Bilder in einem Plot anordnen

# Bildabmessungen und globale Plot-Parameter
rcParams.update({
    'figure.figsize'    : [14,10],
    'text.usetex'       : True,
    'axes.titlesize' : 14,
    'axes.labelsize' : 16,
    'xtick.labelsize' : 14 ,
    'ytick.labelsize' : 14
})

h_list = np.genfromtxt("./Numerical_Differentiation_2.dat", max_rows=1)   # Einlesen der benutzten x-Werte der Stuetzstellen
data = np.genfromtxt("./Numerical_Differentiation_2.dat", skip_header=20) # Einlesen der berechneten Daten von Numerical_Differentiation_2.cpp

fig = plt.figure()                                                        # Hauptbild
gs = gridspec.GridSpec(2, 2, width_ratios=[1,1], wspace=0.3, hspace=0.3)  # Anordnung der vier Unterbilder
axs = [plt.subplot(gs[i]) for i in range(4)]                              # Liste der Unterbilder

titles = ['Zweipunkteformel', 'Dreipunkte-Endpunkt-Formel',    # Titel der Unterbilder
          'Dreipunkte-Mittelpunkt-Formel',
          'Fünfpunkte-Mittelpunkt-Formel']
l_width=0.8                                                    # Festlegung der Plot-Liniendicke
alp=0.7                                                        # Festlegung der Transparenz der Kurven
labels = [r'$\rm h=' + f'{h:.3f}' + r'$' for h in h_list[:3]]  # Plot-Labels fuer die unterschiedlichen h-Werte
colors = ["blue", "red", "green"]                              # Festlegung der Farben
colums = [3,7,11]                                              # Spalten der Kurven mit untersch. h-Werten

for j in range(4):
    for i in range(3):
        axs[j].plot(data[:,1],data[:,colums[i]+j], color=colors[i], linewidth=l_width, linestyle='-', alpha=alp, label=labels[i])
    axs[j].plot(data[:,1],data[:,2], color="black", linewidth=l_width, linestyle='-.', alpha=alp, label=r'$\rm Wirklicher \, Wert$')
    axs[j].set_title(titles[j])                                # Titel
    axs[j].set_xlabel(r"$\rm x$")                              # x-Label
    axs[j].set_ylabel(r"$\rm f'(x)$")                          # y-Label
    axs[j].legend(frameon=True, loc="lower right",fontsize=10) # Anordnung der Legende

plt.savefig("Numerical_Differentiation_2.png", dpi=400, bbox_inches="tight", pad_inches=0.05, format="png") # Speichern der Abbildung als Bild
plt.show()
