Visualizing time series

Introduction to Data Visualization with Julia

Gustavo Vieira Suñe

Data Analyst

Time series

A line chart displaying tomato prices as a function of time. The line shows a time series with a positive trend and high variability.

Introduction to Data Visualization with Julia

Tomato prices

  • tomato DataFrame
Date Price (Rupees)
FEB-2004 10.0
MAR-2004 12.0
APR-2004 6.0
MAY-2004 8.0
... ...
  • Sort dates
    tomato.Date = sort(
      tomato.Date, :Date
    )
    
# Plot time series
plot(
    tomato.Date,
    tomato.Price,

# Customize the plot linewidth=2, linecolor=:tomato, label="Tomato" ) xlabel!("Date") ylabel!("Unit Price (Rupees)")
Introduction to Data Visualization with Julia

Tomato prices

A line chart displaying tomato prices as a function of time. The x-axis labels overlap each other.

  • tomato DataFrame
Date Price (Rupees)
APR-2004 6.0
APR-2005 10.0
APR-2006 8.0
APR-2007 8.0
... ...
  • Date column has strings!
Introduction to Data Visualization with Julia

Dates with Julia

using Dates

birthday = Date("1989-12-04")
  • Other formats
birthday = Date(
    "1989/DEC/04", dateformat"y/u/d"
)
birthday = Date(
    "Dec 4, 1989", dateformat"u d, y"
)
Code Match Examples
Y/y Year (YYYY) 1989, 2023
m Month (MM) 1, 10
u Abbreviated Month Jan, DEC
U Month Name January, DECEMBER
d Day (DD) 4, 28
H Hour (HH) 12, 22
M Minute (MM) 05, 25
S Second (SS) 10, 59
1 https://docs.julialang.org/en/v1/stdlib/Dates/#Period-Types
Introduction to Data Visualization with Julia

Tomato prices with Dates

  • Convert strings to Dates
tomato.Date = Date.(
    tomato.Date, dateformat"u-y"
)
  • Sort by date
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
... ...
Introduction to Data Visualization with Julia

Tomato price time series

# Plot time series
plot(
    tomato.Date,
    tomato.Price,
    # Customize the plot
    linewidth=2,
    linecolor=:tomato,
    label="Tomato"
)
xlabel!("Date")
ylabel!("Unit Price (Rupees)")

A line chart displaying tomato prices as a function of time. The x-axis labels are formatted as dates and do not overlap each other.

Introduction to Data Visualization with Julia

Annotating a plot

# Row with highest price 
maximum_price = tomato[
    argmax(tomato.Price), :]

# Annotate the plot annotate!( # Coordinates maximum_price.Date, maximum_price.Price + 2,
# Annotation text "Highest Price", annotationfontsize=8 )

A line chart displaying tomato prices as a function of time. The point with the highest price is indicated with an annotation.

Introduction to Data Visualization with Julia

Let's practice!

Introduction to Data Visualization with Julia

Preparing Video For Download...