1. 程式人生 > >使用numpy pandas matplotlib畫圖,dataframe字串格式轉化為時間格式

使用numpy pandas matplotlib畫圖,dataframe字串格式轉化為時間格式

源資料,16032筆:

主要針對mean欄畫圖,針對start轉換為時間格式,程式碼如下:

import numpy as np
import pandas as pd
import xlrd
import datetime
import matplotlib.pyplot as plt
#獲取內容
df = pd.read_excel(r"C:\Users\mayn\Desktop\ArrayBP\BPLack.xlsx",sheet_name="BP")
#轉化int型別to str型別
df["start"] = df["start"].astype(str)
#轉化日期格式
df["start"] = df["start"].apply(
    lambda x:datetime.datetime.
        strptime(x[:4]+"-"+x[4:6]+"-"+x[6:8]+" "+x[8:10]+":"+x[10:12]+":"+x[12:],
                  "%Y-%m-%d %H:%M:%S"))
print(df["start"].head())

#sub_axix = filter(lambda x:x%500==0,df["start"])
plt.figure(figsize=(12,6))
plt.title('Result Analysis')
plt.plot(df["start"], df["mean"], color='c', label='CTC-Array 1AWEG030-H3PO4 Chart')
plt.legend()
plt.xticks(rotation=200)
plt.xlabel=("DateTime")
plt.ylabel=("H3P04")
plt.show()

結果如圖: