時間序列視覺化

Julia 資料視覺化入門

Gustavo Vieira Suñe

Data Analyst

時間序列

折線圖:番茄價格隨時間變化。此時間序列呈正向趨勢且波動大。

Julia 資料視覺化入門

番茄價格

  • tomato DataFrame
Date Price (Rupees)
FEB-2004 10.0
MAR-2004 12.0
APR-2004 6.0
MAY-2004 8.0
... ...
  • 依日期排序
    tomato.Date = sort(
      tomato.Date, :Date
    )
    
# 繪製時間序列
plot(
    tomato.Date,
    tomato.Price,

# 自訂圖表 linewidth=2, linecolor=:tomato, label="Tomato" ) xlabel!("Date") ylabel!("Unit Price (Rupees)")
Julia 資料視覺化入門

番茄價格

折線圖:番茄價格隨時間變化。x 軸標籤彼此重疊。

  • tomato DataFrame
Date Price (Rupees)
APR-2004 6.0
APR-2005 10.0
APR-2006 8.0
APR-2007 8.0
... ...
  • Date 欄位是字串!
Julia 資料視覺化入門

Julia 的日期

using Dates

birthday = Date("1989-12-04")
  • 其他格式
birthday = Date(
    "1989/DEC/04", dateformat"y/u/d"
)
birthday = Date(
    "Dec 4, 1989", dateformat"u d, y"
)
代碼 對應欄位 範例
Y/y 年(YYYY) 1989, 2023
m 月(MM) 1, 10
u 縮寫月 Jan, DEC
U 月名 January, DECEMBER
d 日(DD) 4, 28
H 時(HH) 12, 22
M 分(MM) 05, 25
S 秒(SS) 10, 59
1 https://docs.julialang.org/en/v1/stdlib/Dates/#Period-Types
Julia 資料視覺化入門

用 Date 的番茄價格

  • 將字串轉為 Date
tomato.Date = Date.(
    tomato.Date, dateformat"u-y"
)
  • 依日期排序
tomato.Date = sort(
    tomato.Date, :Date
)
Date Price (Rupees)
2004-02-01 10.0
2004-03-01 12.0
2004-04-01 6.0
2004-05-01 8.0
... ...
Julia 資料視覺化入門

番茄價格時間序列

# 繪製時間序列
plot(
    tomato.Date,
    tomato.Price,
    # 自訂圖表
    linewidth=2,
    linecolor=:tomato,
    label="Tomato"
)
xlabel!("Date")
ylabel!("Unit Price (Rupees)")

折線圖:番茄價格隨時間變化。x 軸標籤為日期格式且不重疊。

Julia 資料視覺化入門

圖上加註解

# 最高價格的列
maximum_price = tomato[
    argmax(tomato.Price), :]

# 加上註解 annotate!( # 座標 maximum_price.Date, maximum_price.Price + 2,
# 註解文字 "Highest Price", annotationfontsize=8 )

折線圖:番茄價格隨時間變化。最高價位置以註解標示。

Julia 資料視覺化入門

一起來練習吧!

Julia 資料視覺化入門

Preparing Video For Download...