# Date axis in Makie

**URL:** https://discourse.julialang.org/t/date-axis-in-makie/63430
**Category:** New to Julia
**Tags:** plotting, makie
**Created:** [June 23, 2021, 9:40am UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430 "2021-06-23T09:40:33Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [June 23, 2021, 9:40am UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/1 "2021-06-23T09:40:33Z")

</div>

I was looking into Makie for plotting some time series but could only find an [open issue](https://github.com/JuliaPlots/Makie.jl/issues/442) for having dates on one of the axes. The first suggested fix does not work for me (I get an error when calling `lines`) and the second seems very verbose. Are there any better options? Maybe with AlgebraOfGraphics?  
EDIT:  
In addition the second fix leads to weird output in a data example here:

```julia
using CairoMakie, CSV, DataFrames, Dates
using PlotUtils: optimize_ticks
readurl(url) = download(url) |> CSV.File |> DataFrame;
datef = dateformat"yyyy-mm-ddTHH:MM:SS+ss:ss";
vaccinations = readurl("https://info.gesundheitsministerium.at/data/timeline-eimpfpass.csv");
vaccinations.date = Date.(vaccinations.Datum, datef);
subset!(vaccinations, :Name => ByRow(==("Österreich")));
fig = Figure()
ax = Axis(fig[1,1])
dateticks = optimize_ticks(vaccinations.date[1], vaccinations.date[end])[1]
lines!(ax, datetime2rata.(vaccinations.date), vaccinations.EingetrageneImpfungen);
ax.xticks[] =(datetime2rata.(dateticks) , Dates.format.(dateticks, "mm/dd/yyyy"));

```

 ![test](https://global.discourse-cdn.com/julialang/original/3X/b/5/b583032aefa4dca98f9327b5deaf6edd1d3ec0cc.png)

Thanks!

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [June 23, 2021, 10:44am UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/2 "2021-06-23T10:44:40Z")

</div>

The problem boils down to this: If you can plot more things into an existing axis, what happens if you plot into a “date axis”. Should you be able to plot only dates? Should it be like a normal axis just with ticks that look like dates? How should the conversion from dates to numbers work? This is all not worked out yet, which is why we don’t have one polished approach. It’s easier for Plots.jl where you assemble a plot top-down once.

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [June 23, 2021, 10:55am UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/3 "2021-06-23T10:55:59Z")

</div>

Gottcha thanks! Unfortunately, I have not idea how that “should” be done. Let me know if I can contribute (e.g. test) something to this topic.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [June 23, 2021, 1:22pm UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/4 "2021-06-23T13:22:58Z")

</div>

AlgebraOfGraphics would actually be in a better position to deal with this, as its plots are also made in a more top-down style.

---

<div class="post-metadata">

### Author: ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)
#### Post date: [June 23, 2021, 2:22pm UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/5 "2021-06-23T14:22:21Z")

</div>

Currently, I do it manually, like here:  
[https://lazarusa.github.io/BeautifulMakie/ScattersLines/timeSeries/](https://lazarusa.github.io/BeautifulMakie/ScattersLines/timeSeries/)

---

<div class="post-metadata">

### Author: ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)
#### Post date: [June 24, 2021, 12:23pm UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/6 "2021-06-24T12:23:52Z")

</div>

Thank you that works great! For completeness here is the example above using @lazarusA’s suggestion

```julia
using CairoMakie, CSV, DataFrames, Dates
using PlotUtils: optimize_ticks
readurl(url) = download(url) |> CSV.File |> DataFrame;
datef = dateformat"yyyy-mm-ddTHH:MM:SS+ss:ss";
vaccinations = readurl("https://info.gesundheitsministerium.at/data/timeline-eimpfpass.csv");
vaccinations.date = Date.(vaccinations.Datum, datef);
subset!(vaccinations, :Name => ByRow(==("Österreich")));
fig = Figure()
ax = Axis(fig[1,1])
days = length(vaccinations.EingetrageneImpfungen)
lines!(ax, 1:days, vaccinations.EingetrageneImpfungen);
ax.xticks = (1:7:days, string.(vaccinations.date)[1:7:days])
ax.xticklabelrotation = π/4
fig

```

 ![test](https://global.discourse-cdn.com/julialang/original/3X/9/9/990722b0785f16e69d2dfd7073bf1f3a07df1f1f.png)

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [June 27, 2021, 4:22pm UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/7 "2021-06-27T16:22:45Z")

</div>

> [@jules](#):
>
> The problem boils down to this: If you can plot more things into an existing axis, what happens if you plot into a “date axis”. Should you be able to plot only dates? Should it be like a normal axis just with ticks that look like dates? How should the conversion from dates to numbers work? This is all not worked out yet, which is why we don’t have one polished approach.

I think maybe `Axis` should have a notion of a unit / be unit-aware. Axes are already associated to numbers (limits and ticks and such) so I think those numbers having a unit would be pretty natural. So if say `Axis`’s had Unitful units, then plotting into an axis with different units would try to convert and if it can’t convert, would fail. And if you want to plot a bunch of stuff with incompatible units on the same axis, just `ustrip` them first. (This is easier now that Unitful 1.7 interoperates with Dates).

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [June 27, 2021, 6:22pm UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/8 "2021-06-27T18:22:00Z")

</div>

AlgebraOfGraphics supports this already (see [time series example](http://juliaplots.org/AlgebraOfGraphics.jl/dev/generated/gallery/#Time-series)). The machinery for this lives in PlotUtils, so [this method](https://github.com/JuliaPlots/AlgebraOfGraphics.jl/blob/master/src/scales.jl#L77) seems sufficient.

I like @ericphanson’s idea that the tick machinery could interoperate with dates (and other unitful quantities) via Unitful. I think here handling this is not as bad as the categorical conversion we used to have, because there is a well defined transformation from timestamps to floats.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [June 27, 2021, 7:48pm UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/9 "2021-06-27T19:48:09Z")

</div>

One problem with timestamp to float is our float32 conversion. That would not work for nanoseconds or microseconds I think, maybe even larger units, if the distances between the numbers are too small relative to the absolute values.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 10, 2022, 12:09pm UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/10 "2022-05-10T12:09:59Z")

</div>

Just happened to notice that the manual workaround doesn’t work when we need to zoom in.

> [@Zooming and date axes in Makie](https://discourse.julialang.org/t/zooming-in-makie/80790):
>
> Hi there! I recently got acquainted with Makie and I absolutely love it! Here are 2 small questions related to zooming (in GLMakie): Is it possible to update the ticks dynamically when zooming? For instance I would like ticks at 10, 20 and 30 with the default view, but when zooming by x10 I would like ticks at 1, 2 and 3 Is it possible to synchronize zooming on several axes? For instance, I have a stack of 3 axes with the same x axis. When I select a rectangle region in the first one, I woul…

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [January 23, 2024, 10:30am UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/11 "2024-01-23T10:30:41Z")

</div>

Could we update this link? I think the current one is in here: [Beautiful Makie](https://beautiful.makie.org/examples/2d/lines/line_time)

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [January 23, 2024, 11:33am UTC](https://discourse.julialang.org/t/date-axis-in-makie/63430/12 "2024-01-23T11:33:14Z")

</div>

This is still way more painful than it should be. I agree a general unit aware axis would be good, but I think that should be separate. If the x axis is in pounds, I can use real numbers for the axis and put pounds in the label. Dates are fundamentally different in this regard.
