# Plots with formatted DateTime xticks?

**URL:** <https://discourse.julialang.org/t/plots-with-formatted-datetime-xticks/48649>\
**Category:** Visualization\
**Tags:** dates, plots\
**Created:** [October 19, 2020, 6:14pm UTC](https://discourse.julialang.org/t/plots-with-formatted-datetime-xticks/48649 "2020-10-19T18:14:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 19, 2020, 6:14pm UTC](https://discourse.julialang.org/t/plots-with-formatted-datetime-xticks/48649/1 "2020-10-19T18:14:45Z")

</div>

I need to plot some data with `DateTime` type along the abscissa, and would like to have a nice formatting of the `xtick` argument.

Consider this example:

```julia
using Plots, Dates
#
function timeperiod(x::Float64)
    Second(Int(floor(Second(Day(1)).value * x)))
end
#
today = DateTime(2010,10,19)
x = range(0,60,length=100)
tm = today+timeperiod.(x);
#
plot(tm,sin.(2pi*x/30))

```

which gives:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/5/154328ab1f9eba10381fefb7a0aea4c27f11670a.png)

I’d like to indicate the time every week, so I try with:

```julia
tm_tick = range(tm[1],tm[end],step=Day(7))
#
plot(tm,sin.(2pi*x/28),xtick=tm_tick,xrot=60)

```

leading to:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/9/69239abeca49890d2b7e8122a1a10e725d46992b.png)

As seen, this provides unnecessary time resolution (ms?) and the year “disappears”.

So I try with:

```julia
julia> Dates.format.(tm_tick,"yyyy-mm-dd")
5-element Array{String,1}:
 "2010-10-19"
 "2010-11-02"
 "2010-11-16"
 "2010-11-30"
 "2010-12-14"

```

which would be an ok formatting. HOWEVER, if I try with:

```julia
plot(tm,sin.(2pi*x/28),xtick=Dates.format.(tm_tick,"yyyy-mm-dd"),xrot=60)

```

the plot is produced _without_ xticks…

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/e/2ee9f22ed133ee3c085d2f2e408f0263838573db.png)

**Question** : How can I achieve the desired formatting of the xticks?

[I need to abscissa values `tm` to be of type `DateTime` and not `Date`, because I have much higher resolution than once a day. For my use, the `x` vector will not have equidistant elements.]

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 19, 2020, 7:16pm UTC](https://discourse.julialang.org/t/plots-with-formatted-datetime-xticks/48649/2 "2020-10-19T19:16:38Z")

</div>

The `xticks` option receives both the positions and labels of the ticks, so you can do:

```julia
julia> ticks = Dates.format.(tm_tick,"yyyy-mm-dd");

julia> plot(tm,sin.(2pi*x/28),xticks=(tm_tick,ticks),xrot=60)

```

![test](https://global.discourse-cdn.com/julialang/original/3X/3/7/373824ea17f055ff2ffcc543a0f51db4b403eef2.png)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 19, 2020, 7:30pm UTC](https://discourse.julialang.org/t/plots-with-formatted-datetime-xticks/48649/3 "2020-10-19T19:30:07Z")

</div>

Great! Thanks!  
[Eh… I missed `today` by 10 years… 😊]

---

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [January 16, 2024, 7:05am UTC](https://discourse.julialang.org/t/plots-with-formatted-datetime-xticks/48649/4 "2024-01-16T07:05:09Z")

</div>

What if I want to use the default positions? In other words, just change the date format?
