Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib
- matplotlib.use('TkAgg')
- import numpy as np
- from numpy import mean
- from tkinter import filedialog as FD
- from scipy import stats
- import plotly.plotly as py
- import matplotlib.pyplot as plt
- from matplotlib import pylab
- filename = FD.askopenfilename()
- print(filename)
- # opening *into an object* file to read
- file = open(filename, "r")
- # file.readline = reading the file line by line (.read is everything)
- title = file.readline()
- # assign title to substring
- title = title[7:]
- file.readline()
- # read date
- date = file.readline()
- date = date[16:]
- file.readline()
- file.readline()
- file.readline()
- print(title)
- print(date)
- # make blank list, read
- blanks = []
- keep_going = True
- current_line = file.readline()
- while keep_going == True:
- if current_line == "\n":
- keep_going = False
- else:
- blanks.append(float(current_line))
- current_line = file.readline()
- print(blanks)
- # reads the blanks
- file.readline()
- file.readline()
- keep_going = True
- current_line = file.readline()
- # making two arrays
- x = []
- y = []
- # making a loop for the table
- while keep_going:
- if current_line == "":
- keep_going = False
- else:
- li = current_line.split("\t")
- xi = float(li[0])
- for yi in li[2:]:
- x.append(xi)
- y.append(float(yi))
- current_line = file.readline()
- x = np.array(x, dtype=np.float64)
- y = np.array(y, dtype=np.float64)
- print(x)
- print(y)
- # generate linear fit will say a comment and a website will show up, please ignore. This code came from a website.
- # as well as my coding. Not sure how to get rid of the website or comment
- slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
- line = slope * x + intercept
- plt.plot(x,y, line)
- pylab.title('Linear Fit with Matplotlib')
- ax = plt.gca()
- fig = plt.gcf()
- py.plot_mpl(fig, filename='linear-Fit-with-matplotlib')
- fig, ax = plt.subplots(figsize=(5,4))
- def best_fit_slope_and_intercept(xArray, yArray):
- m = (((mean(xArray) * mean(yArray)) - mean(yArray * xArray)) /
- ((mean(xArray) * mean(xArray)) - mean(xArray * xArray)))
- b = mean(yArray) - m * mean(xArray)
- print("STD deviation of blank=", np.std(blanks))
- print("LOD = ", np.std(blanks / m) * 3)
- return m, b
- print("best_fit result: ", best_fit_slope_and_intercept(x, y))
- m, b = best_fit_slope_and_intercept(x, y)
- Fit_line = [(m * xpt) + b for xpt in x]
- plt.scatter(x, y, color='#003F72')
- plt.plot(x, Fit_line)
- plt.show()
- main.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement