add r2 in plot
This commit is contained in:
@@ -7,6 +7,7 @@ from datetime import datetime
|
||||
import math
|
||||
import matplotlib.pyplot as plt
|
||||
import io
|
||||
from scipy.stats import linregress
|
||||
|
||||
curdir = os.getcwd()
|
||||
path_delim = '/'
|
||||
@@ -186,6 +187,10 @@ if not df.empty:
|
||||
wks1.write(0, 0, 'Solution')
|
||||
wks1.write(0, 1, 'Slope')
|
||||
wks1.write(0, 2, 'Intercept')
|
||||
wks1.write(0, 3, 'R^2')
|
||||
# Create a new DataFrame to store linear fit results
|
||||
df_linearfit_results = pd.DataFrame(columns=['Solution', 'Slope', 'Intercept', 'R^2'])
|
||||
|
||||
row = 1
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
@@ -200,20 +205,27 @@ if not df.empty:
|
||||
|
||||
# Fit a linear regression model
|
||||
coefficients = np.polyfit(concentration_means.index, concentration_means.values, 1)
|
||||
|
||||
# Use linregress to get additional statistics including R-squared
|
||||
slope, intercept, r_value, p_value, std_err = linregress(concentration_means.index, concentration_means.values)
|
||||
|
||||
print(f'Coefficients for {solution}: Slope={coefficients[0]}, Intercept={coefficients[1]}')
|
||||
print(f'For {solution}: Slope={slope:.4f}, Intercept={intercept:.4f}, R^2={r_value**2:.4f}')
|
||||
|
||||
ax.plot(concentration_means.index, concentration_means.values, marker='o', linestyle='-', label=f'Mean Absorbance for {solution}')
|
||||
|
||||
ax.plot(concentration_means.index, np.polyval(coefficients, concentration_means.index), linestyle='--', label=f'Linear Fit for {solution}')
|
||||
|
||||
annotation_text = f'Slope: {coefficients[0]:.4f}\nIntercept: {coefficients[1]:.4f}'
|
||||
annotation_text = f'Slope: {slope:.4f}\nIntercept: {intercept:.4f}\nR^2: {r_value**2:.4f}'
|
||||
ax.text(concentration_means.index[-1] + 5, np.polyval(coefficients, concentration_means.index[-1]), annotation_text, fontsize=10, verticalalignment='center')
|
||||
|
||||
wks1.write(row, 0, solution)
|
||||
wks1.write(row, 1, str(coefficients[0]))
|
||||
wks1.write(row, 2, str(coefficients[1]))
|
||||
wks1.write(row, 1, str(slope))
|
||||
wks1.write(row, 2, str(intercept))
|
||||
wks1.write(row, 3, str(r_value**2))
|
||||
|
||||
# Add the linear fit results to the new DataFrame
|
||||
df_linearfit_results = df_linearfit_results.append({'Solution': solution, 'Slope': slope, 'Intercept': intercept, 'R^2': r_value**2}, ignore_index=True)
|
||||
|
||||
row += 1
|
||||
|
||||
ax.set_title('Mean Absorbance for Each Solution')
|
||||
@@ -224,7 +236,11 @@ if not df.empty:
|
||||
|
||||
imgdata=io.BytesIO()
|
||||
fig.savefig(imgdata, format='png')
|
||||
wks1.insert_image(2,2, '', {'image_data': imgdata})
|
||||
wks1.insert_image(6,0, '', {'image_data': imgdata})
|
||||
|
||||
# Write linear fit results to Excel
|
||||
df_linearfit_results.to_excel(writer, sheet_name="linearfit_results")
|
||||
|
||||
|
||||
### debug each row
|
||||
# # Merge df and df_device_precicion_results on deviceId
|
||||
|
||||
Reference in New Issue
Block a user