Plot: format dates in x axis

Hi there,

I have a question regarding date formatting when using plot with gr() backend.

Two question:

  1. Is there a more julian way to format your dates - here: to only show years - then my last line of code?

  2. Is there a way to specifically set the xticks, so e.g. only show year 2023, 2024 and 2027 on the xaxis (not the xlims, but the labelling of the xticks).

using Plots
using Dates
using Distributions

dts = today():Year(1):today()+Year(20)
vs = rand(Normal(0,1),size(dts,1))
plot(dts, vs, label="")
plot!(xformatter = x -> Dates.format(Date(Dates.UTD(x)), "yyyy"))

I cannot find anything in the documentation regarding this.

Thanks for your help!

Is this what you are looking for:

using Plots, Dates, Distributions

dts = today():Year(1):today()+Year(20)
vs = rand(Normal(0,1),size(dts,1))
tick_years = Date.([2023, 2024, 2027])
DateTick = Dates.format.(tick_years, "yyyy")

plot(dts, vs, label="", xticks=false)
plot!(xticks=(tick_years,DateTick), xtickfontsize=6)

Dates_xtick

Perfect, exactly what I wanted. Thanks for your help! Is this anywhere in the documentation?

Yes, search in the Dates documentation for Dates.format method.
For Plots attributes see this page.