PeriodicalDates.jl
PeriodicalDates.jl copied to clipboard
MonthlyDates.jl: strange behavior in x-axis with long time series
I have a long time series comprising 238 quarterly observations (1960.Q1-2019.Q2)). For teaching, I want to plot them using interactivity with Plots, plotlyjs(), Dates.jl and MontlyDates.jl. However, I have a problem with the x-axis displaying the correct information, and I would be very grateful if someone could help.
Without interactivity, I would just create a 238-element LinRange{Float64} with the dates using, e.g., Period1=LinRange(1960,2019.25,238)
, and with a simple piece of code:
plot(
Period1, [RealGDP RealINV RealCONS],
labels = ["GDP" "INV" "CONS"],
color = [:red :orange :blue],
legend = :topleft,
fg_legend = :transparent,
title = "Real GDP, Investment and Consumption: US (1960.Q1--2019.Q2)",
titlefontsize = 11,
)
I would get the following figure:
I have a problem with this simple approach because the thousands-separating comma distorts the dates (I would not mind 2000.5, but 2,005.5 looks awkward). So I searched and found the MonthlyDates.jl, which looks nice and easy to use. I define the format according to:
Period2 = QuarterlyDate("1960-Q1"):Quarter(1):QuarterlyDate("2019-Q2")
and the output, by collect(Period2)
gives the correct output:
1960-Q1
1960-Q2
1960-Q3
1960-Q4
1961-Q1
1961-Q2
1961-Q3
1961-Q4
1962-Q1
...
A time series can be easily produced by replacing Period1
with Period2
in the code above. The plot looks like this:
There are three problems with this plot that I don't know how to remove:
(i) the chosen dates by default look helpless (why should the first x-axis entry be 1963-Q2?);
(ii) the x-axis only displays the correct dates if we put the cursor right on top of displayed entries (for example, 2000-Q4 looks OK): see figure above.
(iii) If we put the cursor slightly to the left/right of the displayed entries on the x-axis, a string appears that makes no sense (e.g., "8023"); see figure below.
This problem looks like an easy one to solve. I tried many ways but failed. Help will be appreciated.
Fortunately, RecipesBase implemented something recently to work around this kind of issues. Can you write a reproductible example so that I can solve this?
Yes, sure. Thanks a lot.
The code is very simple (I use it in Pluto, but I think the interactivity also works in Jupyter, and certainly works in VSCode or Atom):
#packages
using Plots
using DataFrames
using CSV
#using PlutoDataTable
#using LaTeXStrings
using Dates
using MonthlyDates
plotlyjs()
# reading the data
MyData = CSV.read("Test2_Data.csv", DataFrame)
# using LinRange to create a vector with data entries (not using MonthlyDates.jl)
Period1=LinRange(1960,2019.25,238)
# using MonthlyDates.jl to create QuarterlyDate <: TimeType type
Period2 = QuarterlyDate("1960-Q1"):Quarter(1):QuarterlyDate("2019-Q2")
# Creating variables from the DataFrame
RealGDP = MyData[:, 2]
RealCONS = MyData[:, 3]
RealINV = MyData[:, 4]
RealEXP = MyData[:, 5]
FFR = MyData[:, 6]
U = MyData[:,7]
M2 = MyData[:, 8]
# Plotting with dates created according to MonthlyDates.jl
plot(Period2, [RealGDP RealINV RealCONS],
labels = ["GDP" "INV" "CONS"],
color = [:red :orange :blue],
legend = :topleft,
fg_legend = :transparent,
title = "Real GDP, Investment and Consumption: US (1960.Q1-2019.Q2)",
titlefontsize = 11,
)