How to set DateTime format of xlabel using Makie

I want to get xlabel like “HH:MM”, Part of my code as follows. It doesn’t work as I want, but without throwing out an error. Anyone can give some suggestions?

fig = Figure(; size=(1000, 600))
ax1 = Axis(fig[1, 1], ylabel="H[nT]")
ax2 = Axis(fig[2, 1], ylabel="Z[nT]")

dates = DateTime("2025-03-19"):Hour(3):DateTime("2025-03-20")
tick_positions = datetime2unix.(dates)  # Convert dates to Unix time (positions)
tick_labels = Dates.format.(dates, "HH:MM")  

ax3 = Axis(fig[3, 1],
    xlabel="Time_UT",
    ylabel="D[nT]",
    xticks = (tick_positions, tick_labels),
     yticklabelspace = 36.0 
 )
linkxaxes!(ax1, ax2, ax3)

lines!(ax1, df.time, df.H, color=:red)
hidexdecorations!(ax1, grid=false, label=false) 
lines!(ax2, df.time, df.Z, color=:red)
hidexdecorations!(ax2, grid=false, label=false) 
lines!(ax3, df.time, df.D, color=:red)

fig

The result is like this.

Thanks a lot! Your suggestion works.
Makie cann’t support DateTime Type well. There is another way to avoid this issue, that is, transform DateTime data with datetime2unix function.

fig = Figure(; size=(1000, 600))

ax1 = Axis(fig[1, 1], ylabel="H[nT]")
ax2 = Axis(fig[2, 1], ylabel="Z[nT]")

dates = DateTime("2025-03-19"):Hour(3):DateTime("2025-03-20")
tick_positions = datetime2unix.(dates)  # Convert dates to Unix time (positions)
# tick_labels = Dates.format.(dates)
tick_labels = ["$(Dates.format(t, "HH:MM") )\n $(Dates.format(t, "yyyy/mm/dd") )" for t in dates]

ax3 = Axis(fig[3, 1],
    xlabel="Time_UT",
    ylabel="D[nT]",
    xticks = (tick_positions, tick_labels),
    yticklabelspace = 36.0 
    # xticklabelrotation=pi / 4)
)
linkxaxes!(ax1, ax2, ax3)

lines!(ax1, datetime2unix.(df.time), df.H, color=:red)
hidexdecorations!(ax1, grid=false, label=false)  
lines!(ax2, datetime2unix.(df.time), df.Z, color=:red)
hidexdecorations!(ax2, grid=false, label=false) 
lines!(ax3, datetime2unix.(df.time), df.D, color=:red)

fig

3 Likes

Yes I think in practice the date support in Makie is currently only really useful for quick and dirty exploratory plotting.

While my comment in the GitHub issue can get you some of the way towards a more useable date format by at least letting you customise ticks, it doesn’t fix many other issues (e.g. date support being completely broken for some plot types).

Your solution of converting dates/datetimes to numerical values using datetime2unix and date2epochdays is the way to go in my view at this point.