Я пытался сделать линию наилучшего соответствия на этом графике. Я использую pandas, чтобы извлечь данные из CSV-файлов, чтобы построить данные и сделать линию наилучшего соответствия. Значения dP и Gain (x и y) построены в порядке, и уравнение наилучшего соответствия также является правильным. Каким-то образом линия не показывает. Чего-то не хватает в разделе линии наилучшего соответствия или в графике раздела данных? Кроме того, как я могу преобразовать уравнение наклона в научную нотацию c?
print('\n'*1000)
from math import sqrt
import numpy as np
import matplotlib.pyplot as plt
plt.close('all')
# Finding data files for the pressure transducer
import os #Manipulate file systems
import pandas as pd
directory = os.fsencode('RawDataFiles')
filenames = [] # Set up 'filenames' as an empty list
#Loop through the subdirectry and add the .csv files to our filename list
for file in os.listdir(directory):
fname = os.fsdecode(file)
if fname.endswith('.csv'):
filenames.append(fname)
#Construct an empty dataframe for the data needed
alldata = pd.DataFrame(columns =
["filename","Vin","dVin","Vout","dVout","Gain","dGain","dH","dP","ddP"])
#Load info about the manometer
dHs = pd.read_csv("dH.csv")
for f in filenames:
pfile= pd.read_csv("RawDataFiles/"+f)
Vexcite = pfile["V_excite/V"]
Vout = pfile["V_out/V"]
Gain = np.divide(Vout,Vexcite)
Vexcite_nom = Vexcite.mean()
Vout_nom = Vout.mean()
Gain_nom = Gain.mean()
Vexcite_std = Vexcite.std()
Vout_std = Vout.std()
Gain_std = Gain.std()
#Add info from the manometer
#Determine which dH corresponds to this file
deeH = dHs.loc[dHs['filename'] == f, 'dH'].iloc[0] #Gives the dH that was recorded in the 2nd file
#constants
rho= 0.825 #kg.m^3
g= 9.803 #m/s^2
#uncertainties
drho= 0.0005
dg= 0.0005
ddeeH= 1/12
#Conversion to Si
deeH= deeH*1
ddeeH= ddeeH*1
#convert to dP and uncertainty
dP= rho*g*deeH
if deeH > 0:
ddP = dP*sqrt((drho/rho)**2 + (dg/g)**2 + (ddeeH/deeH)**2)
else:
ddP = 0
#Plot the Data
alldata.plot(kind ="scatter", x='dP', y="Gain", color="red", marker='x', xerr= "ddP",
yerr="dGain",label= ('Measurements'))
plt.title('Calibration Data for a Pressure Transducer')
plt.xlabel('dP/Pa')
plt.ylabel('Gain')
#Best fit line
pfit = np.polyfit(alldata["dP"], alldata["Gain"],1)
Pplot = np.array([dP.min(), dP.max()])
Gplot = Pplot * pfit[0] + pfit[1]
linfit = plt.plot(Pplot, Gplot,'b-',label=("Line Fit: Gain =%1.3fdP + %1.3f" %(pfit[0], pfit[1])))
plt.legend()
'' '