如何通过删除斜率来修正体重图表

问题描述 投票:0回答:1

我有一个 .txt 文件,每行有十个分号分隔的值。日期和时间是第一个值,重量是第八个值。根据这些数据,我制作了一个图表,以时间为 x 轴,以克为单位的重量作为 y 轴。我的体重增加有两种不同的来源:一种是铁锈引起的线性增加的重量增加,另一种是我感兴趣的重量增加。如何制作一个校正图表来显示重量增加和线性重量如果我知道斜率的话,除锈后会增加吗?

下面的代码是我尝试过的。图表中重量增加仅由于生锈而增加的部分,并且与我发现的斜率具有相同的斜率,在校正后的重量图表中并不完全平坦,所以我知道这是错误的。如果我将斜率增加到更高的值,它会发生很大的变化。我发现的坡度(steel_slope)是正确的,我已经仔细检查过。

数据链接:https://drive.google.com/file/d/1VkMRRmTfWvhzWGUIgdu3di_DWYpoM2A4/view?usp=sharing

import re
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import convolve


def AddData(file_path):
    
   
   
    with open(file_path, 'r+') as f:
        text = f.read()
        text = re.sub(',', '.', text)
        f.seek(0)
        f.write(text)
        f.truncate
    

    # Read the ';' separated text file into a pandas DataFrame with column names
    df = pd.read_csv(file_path, delimiter=';')
    DATE_AND_TIME='DATE_AND_TIME'
    TEMP ='TEMP'
    N2= 'N2'
    CO2='CO2'
    WEIGHT='WEIGHT'
    column_index_to_drop = [1, 2, 3, 7,  9, 10]
  
    df = df.drop(df.columns[column_index_to_drop], axis=1)

    df.columns = [DATE_AND_TIME,  TEMP, N2, CO2, WEIGHT ]
    df[DATE_AND_TIME] = pd.to_datetime(df[DATE_AND_TIME],format ="%d.%m.%y %H:%M:%S")
    df = df.set_index(DATE_AND_TIME)
    return df
    # Display the DataFrame
    

DATE_AND_TIME='DATE_AND_TIME'
TEMP ='TEMP'
N2= 'N2'
CO2='CO2'
WEIGHT='WEIGHT'
data_dict = {
             "300424": AddData('300424.txt')
             }


#%%
info_dict ={
            "300424":["300424","2024.05.01 09:10:00","2024.05.03 10:00:08", "5 vol% $CO_2$ 30/4",  0.0782, 36, 244.75, 11.74, 530, 765]
            }


#%%
Date =  "300424" 

#data_dict[Date].plot( y=WEIGHT )
#plt.show()

steel_slope = 0.0034369
corrected_weight = data_dict["300424"][WEIGHT].copy() 

for index, WEIGHT in corrected_weight.items():
    steel_weight_contribution = steel_slope * WEIGHT
    corrected_weight[index] -= steel_weight_contribution


plt.plot(corrected_weight)
plt.xlabel('Time')
plt.ylabel('Corrected Weight')
plt.show()
python for-loop plot
1个回答
0
投票

加载所需列后得到此表:

                           temp      n2     co2  weight
date                                                   
2024-04-30 09:58:46  9900347.25 -0.0024  0.0000  7029.9
2024-04-30 09:58:48  9900240.73 -0.0022  0.0016  7029.9
2024-04-30 09:58:50  9900351.57 -0.0022 -0.0012  7030.0
2024-04-30 09:58:52  9900029.66 -0.0017  0.0013  7213.4
2024-04-30 09:58:54  9900151.18 -0.0009  0.0016  7030.3
...                         ...     ...     ...     ...
2024-05-04 15:27:32      938.64  0.0977  0.0013  7108.0
2024-05-04 15:27:34      938.87  0.0975  0.0010  7107.8
2024-05-04 15:27:36      938.81  0.0977  0.0016  7108.2
2024-05-04 15:27:38      938.80  0.0978  0.0029  7108.0
2024-05-04 15:27:40      938.77  0.0978  0.0013  7108.2

当我绘制体重与日期的关系图时,我得到的是:

enter image description here

如果我将其限制在(“2024.05.01 09:10:00”,“2024.05.03 10:00:08”)之间,它只是一条平线。

您能确认这就是原始数据的样子吗?请详细说明您所询问的线性校正 - 我不知道线性部分来自哪里。

我用于加载和绘图的代码:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

df = pd.read_csv(
    'line.txt', sep=';', usecols=[0, 4, 5, 6, 8],
    index_col=0, parse_dates=[0], date_format="%d.%m.%y %H:%M:%S",
    header=None, names=['date', 'temp', 'n2', 'co2', 'weight'],
)
df.head()

df.plot(use_index=True, y='weight', figsize=(6, 3), ylabel='weight', legend=False)
# plt.xlim("2024.05.01 09:10:00", "2024.05.03 10:00:08")
© www.soinside.com 2019 - 2024. All rights reserved.