Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import matplotlib
  2.  
  3. matplotlib.use('TkAgg')
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from numpy import mean
  7. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  8. from matplotlib.figure import Figure
  9. from tkinter import *
  10. from tkinter import filedialog as FD
  11. from scipy import stats
  12.  
  13.  
  14. filename = FD.askopenfilename()
  15. print(filename)
  16. # opening *into an object* file to read
  17. file = open(filename, "r")
  18.  
  19. # file.readline = reading the file line by line (.read is everything)
  20. title = file.readline()
  21. # assign title to substring
  22. title = title[7:]
  23.  
  24. file.readline()
  25. # read date
  26. date = file.readline()
  27. date = date[16:]
  28.  
  29. file.readline()
  30. file.readline()
  31. file.readline()
  32.  
  33. print(title)
  34. print(date)
  35.  
  36. # make blank list, read
  37. blanks = []
  38.  
  39. keep_going = True
  40. current_line = file.readline()
  41.  
  42. while keep_going == True:
  43. if current_line == "\n":
  44. keep_going = False
  45. else:
  46. blanks.append(float(current_line))
  47. current_line = file.readline()
  48.  
  49. print(blanks)
  50.  
  51. # reads the blanks
  52. file.readline()
  53. file.readline()
  54.  
  55. keep_going = True
  56. current_line = file.readline()
  57.  
  58. # making two arrays
  59. x = []
  60. y = []
  61.  
  62. # making a loop for the table
  63. while keep_going:
  64. if current_line == "":
  65. keep_going = False
  66. else:
  67. li = current_line.split("\t")
  68.  
  69. xi = float(li[0])
  70. for yi in li[2:]:
  71. x.append(xi)
  72. y.append(float(yi))
  73. current_line = file.readline()
  74.  
  75. x = np.array(x, dtype=np.float64)
  76. y = np.array(y, dtype=np.float64)
  77. print(x)
  78. print(y)
  79.  
  80. # generate linear fit
  81. slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
  82. line = slope * x + intercept
  83.  
  84.  
  85. # setting up graph
  86. main = Tk()
  87. main.geometry("266x208+300+300")
  88. main.title("geometry")
  89. #fig = Figure(figsize=(5, 4), dpi=100)
  90. #ax = fig.add_subplot(111)
  91. fig, ax = plt.subplots(2, 2, figsize=(15, 15))
  92. #ax.scatter(x, y)
  93.  
  94. #plt.plot(x, y, 'o', x, line)
  95. #main.title('Linear Fit with MatPlotLib')
  96.  
  97. # print(blankData)
  98. print("blank deviation=", np.std(blanks))
  99. print("LOD = ", np.std(blanks) * 3)
  100.  
  101. def best_fit_slope_and_intercept(xArray, yArray):
  102. m = (((mean(xArray) * mean(yArray)) - mean(yArray * xArray)) /
  103. ((mean(xArray) * mean(xArray)) - mean(xArray * xArray)))
  104.  
  105. b = mean(yArray) - m * mean(xArray)
  106.  
  107. return m, b
  108.  
  109.  
  110. print("best_fit result: ", best_fit_slope_and_intercept(x, y))
  111. m, b = best_fit_slope_and_intercept(x, y)
  112.  
  113. Fit_line = [(m * xpt) + b for xpt in x]
  114. plt.scatter(x, y, color='#003F72')
  115. plt.plot(x, Fit_line)
  116.  
  117. plt.show()
  118. main.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement