# Time tick issue on x-axis

**URL:** <https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643>\
**Category:** Visualization\
**Tags:** question\
**Created:** [September 28, 2018, 8:25pm UTC](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643 "2018-09-28T20:25:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 28, 2018, 8:25pm UTC](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643/1 "2018-09-28T20:25:03Z")

</div>

I’m trying to plot a time-related histogram where the x-axis is times in a day, but I want the ticks to increase every 30 minutes instead of the crazy amount of ticks defaulted by Gadfly. ![test](https://global.discourse-cdn.com/julialang/original/3X/c/1/c176a5fb3ba3e3b8530c0df6f1830f96002d7920.png)

Any other plotting package solution are also welcomed, Gadfly seems to be unable to handle `Time` from `DateTime`.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [September 28, 2018, 9:21pm UTC](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643/2 "2018-09-28T21:21:21Z")

</div>

Plots should be able to do that, but it’d be easier to check if you could post an MWE.

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [September 29, 2018, 1:40am UTC](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643/3 "2018-09-29T01:40:51Z")

</div>

Here’s a quick solution (probably can be improved on). It will work with `DateTime` or `Time` types:

```julia
import Cairo
using Dates, Gadfly

halfhour(x) = hour.(x) + (minute.(x).>30)*0.5

n = 1000
date1 = DateTime(2018,1,1) + Hour.(rand(0:23, n)) + Minute.(rand(0:59, n)) + Second.(rand(0:59,n))
time1 = Time.(date1) 

xticks = Guide.xticks(ticks=[0:2:24;])
pa = plot(x=halfhour.(date1), Geom.histogram(bincount=48), xticks)
pb = plot(x=halfhour.(time1), Geom.histogram(bincount=48), xticks)
draw(PNG(8inch,4inch), hstack(pa, pb))

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [September 29, 2018, 4:42am UTC](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643/4 "2018-09-29T04:42:23Z")

</div>

Just for the record, the inbuilt DateTime axis handling seems broken for histograms in Plots as well (so you could do something like the above, but it won’t do it automatically).

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [September 30, 2018, 12:30am UTC](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643/5 "2018-09-30T00:30:04Z")

</div>

With [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl) it is easy to bin by hour:

```julia
using VegaLite, DataFrames, Dates

n = 1000
df = DataFrame(time = DateTime(2018,1,1) + Hour.(rand(0:23, n)) + Minute.(rand(0:59, n)) + Second.(rand(0:59,n)))

df |> @vlplot(:bar, x="hours(time):o", y="count()")

```

![visualization%20(2)](https://global.discourse-cdn.com/julialang/original/3X/1/3/13fb0898901cfe214b3a6f316882b21779a4373f.png)

I don’t think one can aggregate by half hours, though… The [vega-lite](https://vega.github.io/vega-lite/docs/timeunit.html) documentation generally describes the existing functionality.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [November 25, 2019, 5:18pm UTC](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643/6 "2019-11-25T17:18:55Z")

</div>

I want to make a histogram of Dates. For example from 2017.1.1 till 2018.1.1. How would I do that? And how do I get more than one bar per month?

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [November 25, 2019, 5:41pm UTC](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643/7 "2019-11-25T17:41:15Z")

</div>

Here is the VegaLite.jl way:

```julia
using VegaLite, DataFrames, Dates

n = 1000

 df = DataFrame(time = rand(DateTime(2018,1,1):Hour(1):DateTime(2019,1,1), n))

df |> @vlplot(:bar, x="yearmonthdate(time):o", y="count()")

```

Note how the only difference to the previous example is that I now use the `yearmonthdate` time unit instead of the `hours` time unit. You can see all time units that are supported by vega-lite [here](https://vega.github.io/vega-lite/docs/timeunit.html).

You’d probably want to adjust the width of the bars in this example, otherwise the plot gets a bit wide 🙂

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [November 26, 2019, 10:26pm UTC](https://discourse.julialang.org/t/time-tick-issue-on-x-axis/15643/8 "2019-11-26T22:26:25Z")

</div>

Thank you very much. I really like your work! Keep it up 👍
