# Plot: format dates in x axis

**URL:** <https://discourse.julialang.org/t/plot-format-dates-in-x-axis/56097>\
**Category:** General Usage\
**Tags:** plotting, dates\
**Created:** [February 26, 2021, 3:58pm UTC](https://discourse.julialang.org/t/plot-format-dates-in-x-axis/56097 "2021-02-26T15:58:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [February 26, 2021, 3:58pm UTC](https://discourse.julialang.org/t/plot-format-dates-in-x-axis/56097/1 "2021-02-26T15:58:26Z")

</div>

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).

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

---

<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:** [February 26, 2021, 4:47pm UTC](https://discourse.julialang.org/t/plot-format-dates-in-x-axis/56097/2 "2021-02-26T16:47:10Z")

</div>

Is this what you are looking for:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/f/d/fda96a266f9675ae372d96760c47bfd5bd01ba87.png)

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [February 26, 2021, 4:58pm UTC](https://discourse.julialang.org/t/plot-format-dates-in-x-axis/56097/3 "2021-02-26T16:58:38Z")

</div>

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

---

<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:** [February 26, 2021, 5:12pm UTC](https://discourse.julialang.org/t/plot-format-dates-in-x-axis/56097/4 "2021-02-26T17:12:59Z")

</div>

Yes, search in the [Dates documentation](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.format-Tuple%7BTimeType,AbstractString%7D) for `Dates.format` method.  
For Plots attributes see this [page](https://docs.juliaplots.org/latest/attributes/).
