Currently I am trying to fit a function to a histogram with three datasets. I have got the code for the histogram from the matplotlib gallery (https://matplotlib.org/2.0.2/examples/statistics/multiple_histograms_side_by_side.html). I want to fit my first set of data where #curvefit is written but I don't know, which value I should give for vx because the variable vx is not yet sorted into a histogram and I don't find a value for the sorted vx in that code. That is why currently, the covariance of the parameters can not be determined yet. How can I get or access said new variable?
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
data = np.genfromtxt('C:\\Users\\fabia\\Desktop\\velocities.csv')
vx = data[:,0]
vy = data[:,1]
vz = data[:,2]
Teilchen = 1000
labels = ["vx", "vy", "vz"]
data_sets = [vx,vy,vz]
# Computed quantities to aid plotting
hist_range = (np.min(data_sets), np.max(data_sets))
binned_data_sets = [
np.histogram(d, range=hist_range, bins=Teilchen)[0]
for d in data_sets
]
binned_maximums = np.max(binned_data_sets, axis=1)
x_locations = np.arange(0, sum(binned_maximums), np.max(binned_maximums))
# The bin_edges are the same for all of the histograms
bin_edges = np.linspace(hist_range[0], hist_range[1], Teilchen + 1)
centers = 0.5 * (bin_edges + np.roll(bin_edges, 1))[:-1]
heights = np.diff(bin_edges)
# Cycle through and plot each histogram
fig, ax = plt.subplots()
for x_loc, binned_data in zip(x_locations, binned_data_sets):
lefts = x_loc - 0.5 * binned_data
ax.barh(centers, binned_data, height=heights, left=lefts)
#curvefit
def f(vx, A, k_B, T):
return A*np.exp(-(1/2)*((vx**2)/(k_B*T)))
popt, pcov = curve_fit(f=f, xdata=vx, ydata=5) #ydata is not yet determined
ax.set_xticks(x_locations)
ax.set_xticklabels(labels)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.grid(visible=True, which='major', color='#eeeeee', linestyle='-', linewidth=1.5)
ax.grid(visible=True, which='minor', color='#f7f7f7', linestyle='-', linewidth=0.8)
ax.minorticks_on()
ax.set_axisbelow(True)
ax.set_ylabel("Teilchen")
ax.set_xlabel("Geschwindigkeit")
plt.savefig("Curve_fit.pdf")
This is the current plot I have:
and this is the dataset source: https://drive.google.com/file/d/1mVzYjrx7zm3Xu9vIpkxB9uOJqa4JhuP_/view?usp=sharing