#!/usr/bin/python3 # Beispiel für einen Plot mit linearer Regression from sys import exit from scipy.stats import linregress import matplotlib.pyplot as plt from numpy import array # Messwerte (je eine Liste mit x- und y-Koordinaten) x = [ 0.6, 3.8, 5.5, 7.5, 9.9] # x-Koordinaten y = [-1.7, 1.1, 2.5, 3.8, 8.8] # y-Koordinaten if len(x) != len(y): exit(1) # Fehler -> Abbruch # Berechnung der Ausgleichsgeraden y = a + b*x b,a,r,p,e = linregress(x,y) # lineare Regression print("Anzahl der Datenpunkte:",len(x)) print("Geradensteigung b (slope):",round(b,2)) print("y-Achsenabschnitt (intercept):",round(a,2)) print("Korrelationskoeffizient r:",round(r,2)) print("Bestimmtheitsmaß R^2 = r^2:",round(r*r,2)) print("p-Wert für Signifikanz von b:",round(p,2)) print("Standardfehler von b (stderr):",round(e,2)) # Plot der Messwerte und der Ausgleichsgeraden fd={'fontsize':'medium','fontweight':'bold'} plt.title(str(len(x))+' Messwerte, Ausgleichsgerade \ y = '+str(round(a,2))+' + '+str(round(b,2))+ ' \u00b7 x, R\u00b2 = '+str(round(r*r,2)),fontdict=fd) plt.xlabel('x') ; plt.ylabel('y') plt.plot(x, y, 'o', color='g', markersize=8) plt.plot(x, a+b*array(x), '-', color='tab:brown') plt.savefig(fname='linregress.png')