Seite 1 von 1

Gauß Least Squares Fit und y-Achse normieren

Verfasst: Freitag 28. Juli 2017, 08:19
von sports7

Code: Alles auswählen

import os
import csv
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl #Add "as mpl" for command in line 36
import pylab as py
from scipy.optimize import curve_fit
import xlsxwriter

#Define plot parameters
histNumBins = 150
histAxisMin = 0
histAxisMax = 0.6
plotResol = 600 #dpi

#Set font size/style for all plots
font = {'family' : 'normal',
        'weight' : 'normal',
        'size'   : 8}

#matplotlib.rc('font', **font)
#Get component number centred in histogramm and achieve black edge
mpl.rcParams['patch.force_edgecolor'] = True

#Histogramm-Daten
Inter = [0.46625,	0.47625,0.465625,	0.46875,0.475,0.489375,0.44875,0.490625,0.479375,0.488125,	0.49625,0.4725,0.506875,0.47875,0.473125,	0.463125,0.47625,	0.46375,0.48125,0.4975,0.46125,0.505625,0.479375,0.473125,	0.46875,0.490625,	0.495625,0.5125,0.493125,0.5075,0.48125,0.499375,0.49625,0.476875,0.4775,0.488125,	0.458125,0.494375,0.4875,0.466875,	0.464375,0.465625,0.483125,	0.48375,0.456875,	0.48375,0.4825,0.48375,0.495]
Intermean = np.mean(Inter)
Interstd = np.std(Inter)
Intra = [0.058125]

# Equation for Gaussian
def gaussFunc(x, a, mu, sigma):
    return a * py.exp(-(x - mu)**2.0 / (2 * sigma**2))

#------------------------------------------------------------------------------------
    #Draw histogram
#------------------------------------------------------------------------------------        
fig = plt.figure()
ax1 = fig.add_subplot(111)
  

histIntra = ax1.hist(Intra, histNumBins, range = [histAxisMin, histAxisMax], label = "Intra-Distanz", facecolor = "b")
ax1.set_xlabel("Hamming-Distanz")
ax1.set_ylabel("Ereignisanzahl (Intra-Daten)")
ax1.set_ylim([0, 10])
ax1.tick_params("y", colors = "b")
ax1.grid()
x = np.linspace(histAxisMin, histAxisMax, 500)  
#yAx1 = gaussFunc(x, max(histIntra[0]), intraMean, intraStd)
#plt.plot(x, yAx1, color="orange")    
    
ax2 = ax1.twinx()
histInter = ax2.hist(Inter, histNumBins, range = [histAxisMin, histAxisMax], label = "Inter-Distanzen", facecolor = "g")
ax2.set_ylabel("Ereignisanzahl (Inter-Daten)")
ax2.tick_params("y", colors = "g")
    
#    x = np.linspace(histAxisMin, histAxisMax, 500)
yAx2 = gaussFunc(x, max(histInter[0]), Intermean, Interstd)
plt.plot(x, yAx2, color="orange")

lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc=0)


Ich weiß nicht, ob der Gauß-Fit auf diese Weise ein Least Squares Fit ist? Außerdem möchte ich mir gerne die Funktion des Fits anzeigen lassen. Vor dem eigentlichen Fit möchte ich noch die ax2-Achse auf die Länge von "Inter" normieren.

Kann mir bitte jemand helfen?

VG
sports7

Re: Gauß Least Squares Fit und y-Achse normieren

Verfasst: Samstag 29. Juli 2017, 23:04
von pixewakb
Das scheint gefühlt eine Frage für stackoverflow zu sein, bei matplotlib kennen sich dort m. E. einige mehr aus...