# How to adjust the number and format of DateTime ticks using Plots.jl?

**URL:** <https://discourse.julialang.org/t/how-to-adjust-the-number-and-format-of-datetime-ticks-using-plots-jl/101980>\
**Category:** Visualization\
**Tags:** dates, plots\
**Created:** [July 23, 2023, 7:37pm UTC](https://discourse.julialang.org/t/how-to-adjust-the-number-and-format-of-datetime-ticks-using-plots-jl/101980 "2023-07-23T19:37:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [July 23, 2023, 7:37pm UTC](https://discourse.julialang.org/t/how-to-adjust-the-number-and-format-of-datetime-ticks-using-plots-jl/101980/1 "2023-07-23T19:37:28Z")

</div>

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.

```julia
# 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.

```julia
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 
  ...

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 24, 2023, 5:28am UTC](https://discourse.julialang.org/t/how-to-adjust-the-number-and-format-of-datetime-ticks-using-plots-jl/101980/2 "2023-07-24T05:28:11Z")

</div>

You need to pass the ticks first as numbers, then as strings, ie something like `xticks!((1,2,3), ("ticklabel1", "ticklabel2", "ticklabel3"))`

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 24, 2023, 12:07pm UTC](https://discourse.julialang.org/t/how-to-adjust-the-number-and-format-of-datetime-ticks-using-plots-jl/101980/3 "2023-07-24T12:07:33Z")

</div>

You can try this:

```julia
# (continued)
plot(time_range, y)
t = start_time:Hour(4):end_time
time_ticks = Dates.format.(t,"Ip")
xticks!(Dates.datetime2epochms.(t .- Year(1)), time_ticks)

```

The code above handles some apparent limitation of `xticks!()` fuction with dates, as well as the bug: [the origin of the internal representation of `Date` is not the “epoch day”, but it is off by one year](https://github.com/JuliaPlots/Plots.jl/issues/1944#issuecomment-816988355)

However, if we don’t use the `xticks!()` function, but `plot(; xticks)` key argument, it becomes simpler:

```julia
plot(time_range, y, xticks=(t, time_ticks))

```
