For a project, I have to make a scatter plot and a best fit with errors for an excel file, only when I want to plot the best fit in the scatter plot it is moved to the top of the graph and it doesnt look quite right, I dont know why it does that:
I mounted the drive in a separate cell, the scatter plot is correct, only the graph of the best fit is shifted all we way to the top of the graph.
import matplotlib.pyplot as plt
import numpy as np
from sympy import S, symbols, printing
import scipy.optimize as sci
import pandas as pd
from pathlib import Path
#Importing all the relevant packages for the code, plotting, finding the best fit, analyzing our data and error analysis
file=pd.read_csv(r'/content/drive/MyDrive/Dataset 1(no offset part1 plate 1).csv',sep=";")
file = file.replace(r",", ".", regex=True).astype(float)
x_axis=file['Current I_A1 / A']
y_axis=file['Voltage U_B2 / V']
#Making an array of our calculated Indices of Refraction
plt.title("Hall Voltage vs Current for -10, mT B Field") #Giving our plot a relevant title that also explains what is being plotted so the graph is stand-alone
plt.xlabel("Current [A]") #Giving the x axis a label
plt.ylabel("Hall Voltage [V]") #Giving the y axis a label
plt.grid() #Giving the plot a grid.
def f(x,a,b):
f=a*x+b
return f #Defining our function that is going to be fitted through our data points
def g(x,a,b): #Definin the theoretical function
g=a*x+b
return g
plt.scatter(x_axis,y_axis, color='purple', label= 'measured') #Making a scatter plot of our data points with a Label
fit1, covariance1 = sci.curve_fit(f, x_axis, y_axis) #Using scipy making a best fit throug the data points
plt.plot(x_axis, f(x_axis,*fit1), label='Best Fit') #Displaying the equation of best fit line in the plot #Displaying the equation of best fit line in the plot
plt.legend(loc='best') #Showing the labels belonging to both graphs
plt.show() #Showing the plot
print("n","=",round(fit1[0],4),"*","C" ,"+",abs(round(fit1[1],3))) #Printhing the equation of the best fit and giving the appropiate amount of decimals
print("delta a=",round(np.sqrt(covariance1[0][0]),4)) #Print the uncertainty of the slope of the best fit
print("delta b =",round(np.sqrt(covariance1[1][1]),4)) #Print the uncertainty of the y intercept of the best fit
This is the graph I get
enter image description here
I tried setting different x axis for the curve fit but that made the problem worse