DateListPlot

มีคนถามมาว่ามีข้อมูลตามมด้านล่างนี้จะเอาไป plot ได้ยังไง

20110508000128    1
20110508000315    5
…………. .
20110508235824    x

ซึ่งใน column แรกข้อมูลคือวันที่และเวลาที่อยู่ติดกัน และ column ที่สองก็คือข้อมูลจากการวัดอะไรสักอย่าง

ผมก็บอกคนที่ถามไปว่าไม่ยาก:)  ใน Mathematica ก็น่าจะทำได้

ก่อนอื่นเลยก็ต้องทำการจัดรูปแบบของข้อมูลให้ตรงตามที่ Mathematica ต้องการก่อน โดยที่ผมคิดว่าจะเอาไปใช้กับ

function ชื่อ DateListPlot ครับ ดูรายละเอียดได้ที่ http://reference.wolfram.com/mathematica/ref/DateListPlot.html

อันนี้เป็น function ที่ผมเขียนขึ้นมาใช้ในการเปลี่ยนรูปแบบของข้อมูลครับ

convertdata[yourdatapoint_] := Module[
{output, ls, dat1, dat2, year, month, date, hour, minute, second},
dat1 = yourdatapoint[[1]];
dat2 = yourdatapoint[[2]];
ls = Characters@ToString@dat1;
year = ToExpression@StringJoin@ls[[1 ;; 4]];
month = ToExpression@StringJoin@ls[[5 ;; 6]];
date = ToExpression@StringJoin@ls[[7 ;; 8]];
hour = ToExpression@StringJoin@ls[[9 ;; 10]];
minute = ToExpression@StringJoin@ls[[11 ;; 12]];
second = ToExpression@StringJoin@ls[[13 ;; 14]];
output = {{year, month, date, hour, minute, second}, dat2};
output
];

หรือจะเขียนแบบนี้โดยใช้ StringTake ก็ได้เหมือนกันครับ

convertdata[yourdatapoint_] :=
Module[{output, ls, dat1, dat2, year, month, date, hour, minute,
second},
dat1 = yourdatapoint[[1]];
dat2 = yourdatapoint[[2]];
ls = ToString@dat1;
{year, month, date, hour, minute, second} =
ToExpression /@
StringTake[
ls, {{1, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}, {13, 14}}];
output = {{year, month, date, hour, minute, second}, dat2};
output
];

เอาไปใช้กับข้อมูลก็สามารถทำได้แบบนี้ครับ

data = {{20110508000128, 1}, {20110508000315, 5}, {20110508000411,
10}, {20110508001130, 20}};

DateListPlot[convertdata2 /@dat a, Joined -> True]

 

%d bloggers like this: