Advertisement
Guest User

Untitled

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