How can I extend my line of best fit so that it intercepts the x-axis at y=0 in python?

37 Views Asked by At

I am trying to plot a S shaped curve and then trying to plot a line of best fit for the linear region which should intercept the x axis. I have tried the code below but cannot get the line of best fit to intercept the x axis. I was hoping someone would be able to help please.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit


csv_file_path = "/Users/Desktop/Project 24/GaP measurements/GaP-inital-2610.csv"     


df = pd.read_csv(csv_file_path)    #Reading .csv file for initial intensity with no sample


x = df['X']   # Data points from x column
y = df['Y']   # Data points from y column 


csv_file_path1 = "/Users/Desktop/Project 24/GaP measurements/GaP-200K.csv"


df1 = pd.read_csv(csv_file_path1)    #Reading .csv file for intensity with GaP sample placed in    crysostat window



x1 = df1['X']  # Data points from x column
y1 = df1['Y']  # Data points from y column

T = y1/y   # transmission data points 

wavelength = x[936:1400]  # Sliced x array, wavelength values
transmission = T[750:1214]  # Sliced y array, transmission values


wavel = wavelength*(10**-9)
h = 6.63*(10**-34)  #Planck's constant 
c = 3*(10**8)    # Speed of light
e = (h*c)/wavel   # Equation to calculate the band gap energy
q = 1.6*(10**-19)  # electron charge 
ev = e/q    # converting energy value to electron volts 



R = (1-transmission)/(1+transmission)   #Equation for transmission coefficient 


alpha = (-1/0.0353*10**-3)*np.log(np.sqrt((1-R)**4+(4*transmission**2*R**2))-(1-        R)**2/2*transmission*R**2)   # Equation for absorption coefficient.
plt.plot(ev, alpha, color='grey')  #plot of absorption coefficient as a function of energy
plt.xlabel('energy, eV')
plt.ylabel('absorption coefficient')

plt.figure()
alpha_root = alpha**0.5

plt.plot(ev, alpha_root)





x = ev[100:300]
y = alpha_root[100:300]

slope, intercept = np.polyfit(x, y, 1)

abline_values = [slope * i + intercept for i in x]

# Fit a line to the data
slope, intercept = np.polyfit(x, y, 1)


# Calculate y values for the best fit line
abline_values = [slope * i + intercept for i in x]

# Plot the best fit line over the actual values
plt.plot(x, y, '--', label='Actual Values')
plt.plot(x, abline_values, 'b-', label='Best Fit Line')
plt.plot(x_intercept, 0, color='red', label='X-Intercept')
# Plot the x-intercept as a red point

# Label the axes
plt.xlabel('Photon Energy, eV')
plt.ylabel('Absorption Squared')

# Display a legend
plt.legend()

# Show the plot
plt.show()

I am trying to plot a S shaped curve and then trying to plot a line of best fit for the linear region which should intercept the x axis. I have tried the code below but cannot get the line of best fit to intercept the x axis. I was hoping someone would be able to help please.

0

There are 0 best solutions below