# Histogram of dates in Plots.jl

**URL:** <https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527>\
**Category:** General Usage\
**Tags:** plots\
**Created:** [May 4, 2021, 3:40pm UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527 "2021-05-04T15:40:30Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [May 4, 2021, 3:40pm UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527/1 "2021-05-04T15:40:30Z")

</div>

I’ve come across this problem many times, but I can’t find any mentions of it anywhere on here or the Plots repo. Consider:

```julia
julia> using Plots

julia> histogram(rand(Date(2000, 1, 1):Day(1):Date(2001, 1, 1), 100))

```

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

Here, Plots uses `Dates.value(date)` on the x-axis, which means one has to be pretty good at mental arithmetic to interpret what the graph actually shows. I’ve tried using `xformatter = x -> string(Date(0) + Day(x))` but to no avail (yes, I’ve also forgotten which function gives a `Date` from the `Dates.value` of a date, something I asked Jacob a long time ago on Slack…)

Are there issues related to this that I’ve missed? And does anybody have an idea for a workaround?

---

<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:** [May 4, 2021, 3:58pm UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527/2 "2021-05-04T15:58:58Z")

</div>

Relatedly, I just realized that `histogram` entirely ignores `xformatter` and `yformatter` kwargs:

```julia
julia> histogram(rand(1_000:2_000, 100), yformatter = x -> x/1_000)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/a/b/ab4bc416b0b1f4c36c57db7ef4ece6641b6239c6.png)

but

```julia
julia> plot(rand(1_000:2_000, 100), yformatter = x -> x/1_000)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/a/c/ac63b09329592784f34ac9b05bf0dffbfc8c52d0.png)

---

<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:** [May 4, 2021, 4:54pm UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527/3 "2021-05-04T16:54:12Z")

</div>

This seems to work (_no bells and whistles_):

```julia
using Dates, Plots; gr()

d1, d2 = Date(2000, 1, 1), Date(2001, 1, 1)
d = rand(d1:Day(1):d2, 100)
de = Dates.date2epochdays.(d)
xb = range(Dates.date2epochdays(d1), stop = Dates.date2epochdays(d2), length=5)
xd = Dates.epochdays2date.(round.(Int,xb))

histogram(de, bins = xb, xticks=false, label="Dates histogram")
plot!(xticks=(xb,xd), ylims=(0,40))

```

![Plots_histogram_of_dates](https://global.discourse-cdn.com/julialang/original/3X/0/d/0d6c68ae52633ff83aaf2420f213c6f87f460645.png)

---

<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:** [May 4, 2021, 4:55pm UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527/4 "2021-05-04T16:55:47Z")

</div>

Thanks, although it is of course severly limiting if one has to specify bins manually…

---

<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:** [May 4, 2021, 5:06pm UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527/5 "2021-05-04T17:06:15Z")

</div>

If one knows the exact algorithm used by `histogram()`, one could set the manual value in workaround above equal to the automatic value.  
This could be obtained from a command like:

```julia
nbins = length(fit(Histogram,de).edges[1]) - 1

```

---

<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:** [May 4, 2021, 5:17pm UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527/6 "2021-05-04T17:17:50Z")

</div>

Actually I’ve found an easier workaround: while `histogram` ignores formatters, they can be added to an existing histogram if it is plotted again. So:

```julia
plot(histogram(rand(Date(2000, 1, 1):Day(1):Date(2001, 1, 1), 100)),
    xformatter = x -> Dates.epochdays2date(x+365))

```

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

(Thanks for reminding me of the epochdays2date function!)

---

<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:** [May 4, 2021, 5:22pm UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527/7 "2021-05-04T17:22:02Z")

</div>

@nilshg, could you please verify the plot posted as the dates displayed run back to 1998 while the input data are from 2000 onward.

---

<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:** [May 4, 2021, 5:29pm UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527/8 "2021-05-04T17:29:32Z")

</div>

Indeed - it looks as though `Dates.value` is offset by one year from the `epochdays2date`

```julia
julia> Dates.value(Date(2000, 1, 1))
730120

julia> Dates.epochdays2date(730120)
1999-01-01

```

so above one needs `Dates.epochdays2date(x+365)` to get the right answer

---

<div class="post-metadata">

**Author:** ![js135005](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/js135005/32/8219_2.png) [@js135005](https://discourse.julialang.org/u/js135005)\
**Post date:** [May 6, 2021, 3:06am UTC](https://discourse.julialang.org/t/histogram-of-dates-in-plots-jl/60527/9 "2021-05-06T03:06:22Z")

</div>

I’m pretty sure I ran into this offset by 1 year issue quite a while ago. I ended up defining a conversion function that adjusted by the 1 year at the time.

That code has long since gone by the wayside.
