-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeplots.py
65 lines (53 loc) · 1.74 KB
/
Makeplots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# importing module
from glob import glob
from pandas import *
import time
import sys
import csv
import os.path
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np
import matplotlib.pyplot as plt
import statistics
import math
import scipy
from scipy.stats import norm
from scipy import stats
from math import sqrt
from scipy.optimize import curve_fit
from scipy.optimize import Bounds
def makeplot(data,nbins,xtitle,ytitle,unit,saveTo):
# get total entries before fitting
snorm = len(data)
#print(snorm)
# best fit of data
(mu, sigma) = norm.fit(data)
# the histogram of the data
n, bins, patches = plt.hist(data, nbins, density=False, facecolor='cyan')
#for u in n:
# print('ni=',u)
# x data is bin centres
bin_centres = (bins[:-1] + bins[1:]) / 2
# add a 'best fit' line
binwidth = bins[1]- bins[0]
#print('binwidth=',binwidth)
x = np.linspace(np.min(data), np.max(data), 100)
y = 1.0 / np.sqrt(2 * np.pi * sigma**2) * np.exp(-0.5 * (x - mu) ** 2 / sigma**2)
y = snorm*binwidth*y
l = plt.plot(x, y, 'r--', linewidth=1.5)
# plot
print('mu=',mu)
sign = mu - 3 * sigma
sigp = mu + 3 * sigma
plt.xlabel(xtitle)
plt.ylabel(ytitle)
plt.ylim(1, np.max(n)+3*sqrt( np.max(n) ) )
#plt.yscale("log")
plt.title(f'Mean={mu:.2f}{unit:s}, -3$\sigma$={sign:.2f}{unit:s}, 3$\sigma$={sigp:.2f}{unit:s}')
# Plot the error bars, centred on (bin_centre, bin_count), with length y_error
y_error = [sqrt(i) for i in n]
plt.errorbar(bin_centres, n,yerr=y_error, fmt='ko', label='data', capsize=3)
plt.legend([f'RMS: {sigma:.3f}{unit:s}'])
plt.savefig(saveTo, dpi=100) #do this before plt.show(), otherwise blank plot
plt.show()