I am trying to plot a time series with Plots.jl
. The x axis is in DateTime
format, and I want to format the dates and limit the number of ticks.
The following reproducible example serves to show what I am trying to accomplish.
# Load Packages -----------------------------------------
using Dates
using Plots
#= Now we create data for our example. In my case,
I read a file that has only the `time_range` and the
`y` values =#
start_time = DateTime("2023-01-01T00:00:00");
end_time = DateTime("2023-01-02T18:00:00");
time_range = collect(start_time:Hour(1):end_time);
y = rand(length(time_range));
plot(time_range,y)
# Up to this point, all works as expected. But then, to modify the `xticks`, I do the following.
time_ticks = Dates.format.(collect(start_time:Hour(4):end_time),"Ip");
xticks!(time_ticks)
I want to format the x axis so it only shows the hours in AM/PM format. Also, I don’t need every hour in the date range, only a few hours in between are fine. This is OK because the starting day, month, and year for the plot will go in the title.
The command xticks!
doesn’t work. I’m given the error message below. I don’t understand the error message, though. Thank you for your help.
ERROR: MethodError: no method matching xticks!(::Vector{String})
Closest candidates are:
xticks!(::Union{Symbol, Tuple{AbstractVector{T}, AbstractVector{S}}, AbstractVector{T}} where {T<:Real, S<:AbstractString}; kw...)
@ Plots C:\Users\xxxxx\.julia\packages\Plots\rz1WP\src\shorthands.jl:479
xticks!(::AbstractVector{T}, ::AbstractVector{S}; kw...) where {T<:Real, S<:AbstractString}
@ Plots C:\Users\xxxxx\.julia\packages\Plots\rz1WP\src\shorthands.jl:485
xticks!(::Union{Plots.Plot, Plots.Subplot}, ::Union{Symbol, Tuple{AbstractVector{T}, AbstractVector{S}}, AbstractVector{T}} where {T<:Real, S<:AbstractString}; kw...)
@ Plots C:\Users\xxxxx\.julia\packages\Plots\rz1WP\src\shorthands.jl:487
...