1. 程式人生 > >matplotlib畫多圖,使用python,pandas,matplotlib畫圖監控H3PO4濃度

matplotlib畫多圖,使用python,pandas,matplotlib畫圖監控H3PO4濃度

先上結果:

程式碼:

import numpy as np
import pandas as pd
import xlrd
import datetime
import matplotlib.pyplot as plt

#資料清洗,num為定義母數
def dataArrange(data,num):
    new_data = pd.DataFrame({"time":data["start"],"mean":data["mean"]})
    ls1,ls2,ls3,ls4 = [],[],[],[]
    len_data = len(new_data["time"])
    for i in range(0,int(len_data/num)):
        ls1.insert(0,new_data["time"][len_data-1-i*num])
        #H3PO4 mean
        ls2.insert(0,new_data["mean"][(len_data-1-(i+1)*num):(len_data-1-i*num)].mean())
        #H3PO4 mean: this - last
        if i == 0:
            ls3.insert(0,0)
        else:
            ls3.insert(0,ls2[1]-ls2[0])
        #3 sigma
        ls4.insert(0,new_data["mean"][(len_data-1-(i+1)*num):(len_data-1-i*num)].std()*3)
    return pd.DataFrame({"time":ls1,"mean":ls2,"slope":ls3,"3sigma":ls4})

#時間轉換
def timeTrans(data):
    data["time"] = data["time"].apply(
        lambda x:datetime.datetime.strptime(
            x[0:4]+"-"+x[4:6]+"-"+x[6:8]+" "+x[8:10]+":"+x[10:12]+":"+x[12:14],
            "%Y-%m-%d %H:%M:%S")
    )
    return data

#多圖繪製
def multiChart(data,sigma_spec):
    plt.figure(figsize=(12,10))
    plt.subplot(311)
    plt.title("CTC Array FAB H3PO4 Monitor for BP Lack")
    plt.plot(data["time"],data["mean"],color="b",marker="*")
    plt.subplot(312)
    data["slope_spec"]=0
    plt.title("$mean{(y2-y1)}$")
    plt.plot(data["time"],data["slope"],color="b")
    plt.plot(data["time"],data["slope_spec"],color="r")
    plt.subplot(313)
    data["sigma_spec"]=sigma_spec
    plt.title("3$sigma$")
    plt.plot(data["time"],data["3sigma"],color="b")
    plt.plot(data["time"],data["sigma_spec"],color="r")
    plt.show()


file = r"C:\Users\mayn\Desktop\ArrayBP\BPLack.xlsx"
read_file = pd.read_excel(file)
print(read_file.head())
data = dataArrange(read_file,120)
print(data.head())
data["time"] = data["time"].astype(str)
# data = timeTrans(data)
# print(data.head())
#輸出清洗後資料
file_path=r"C:\Users\mayn\Desktop\ArrayBP\new_data.xlsx"
data.to_excel(file_path,sheet_name="data")
multiChart(data,0.5)

#原始資料

#輸出的資料