Linking subplot x-axes containing dates

I have a strange plotting issue when trying to use subplots. Seperately they seem to plot ok, but when plotting together I was expecting the x-axis to be linked and be identical for both subplots.

Individual plots:

plot_water=plot(Dates.format.(timestamps2,"dd-HH:MM"),waterdepth, xrotation = 60, label="water depth",lw = 3,xticks=10)

plot_pressure=plot(Dates.format.(timestamps,"dd-HH:MM"),pressure,xrotation = 60,label="baro pressure", lw = 3,color=:red,xticks=10)

image
image

Combined plot below, as you can see this combined figure doesn’t make sense.

plot_merged=plot(plot_water,plot_pressure,layout = (2, 1),link=:x)

image

What essential part am I missing?

thanks!

fyi, I’m using the latest version: Version 1.6.1 (2021-04-23)

That looks fine to me, you have 18 minutes more data for baro pressure than for water depth so the plot extends further. You limited the x-axis to 10 ticks, for some reason (maybe because you plotted it first?) those ticks were all used for the shorter dataset.

Maybe try removing the xticks kwarg from the individual plots and move it to the combined plot?

Did you try xlims? See proper syntax for dates here.

thanks for the replies
Not a great success so far:

Maybe try removing the xticks kwarg from the individual plots and move it to the combined plot?

unfortunately that was not making a difference.

Did you try xlims ? See proper syntax for dates here .

maybe I’m not fully following the suggestion entirely, are you referring here to a syntax where automatic min_date and max_date are recognized by Julia or one that should be set manually?

Setting them manually, didn’t really help unfortunately:
plot_merged=plot(plot_water,plot_pressure,layout = (2, 1),link=:x,xlims = Dates.value.((timestamps[1],timestamps[end])))

it provides an error, but than still plots the same as before.
ERROR: type ZonedDateTime has no field instant Stacktrace:

using it in the copy-paste way throws an error and doesn’t plot
xlims = Dates.value.((min_date,max_date))
ERROR: UndefVarError: min_date not defined

thanks

It is recommended to provide a MWE as per guidelines.

The following seems to work:

using Dates, Plots; gr()

# create input data:
dtm1 =  Dates.datetime2epochms.(DateTime(2021,01,11,9,30):Minute(10):DateTime(2021,01,11,21,0))
waterdepth =  rand((1.57:0.1:2.1),length(dtm1))

dtm2 =  Dates.datetime2epochms.(DateTime(2021,01,11,9,30):Minute(10):DateTime(2021,01,12,9,3))
pressure =  rand((1007:0.5:1011),length(dtm2))

# process data:
dtmin, dtmax = minimum([dtm1[1];dtm2[1]]), maximum([dtm1[end];dtm2[end]])
dt_ticks = round.(Int64,LinRange(dtmin,dtmax,10))
str_ticks= Dates.format.(Dates.epochms2datetime.(dt_ticks),"dd-HH:MM")

# plot data:
pw = plot(dtm1,waterdepth, xrotation=60,label="water depth",c=:blue,lw=3,xticks=10)
Plots.plot!(xticks=(dt_ticks, str_ticks), ylims=(1.5,2.2))

pp = plot(dtm2,pressure, xrotation=60,label="baro pressure",c=:red,lw=3,xticks=10)
Plots.plot!(xticks=(dt_ticks, str_ticks), ylims=(1006,1015))

plot_merged = plot(pw,pp, layout=(2, 1), link=:x)
savefig("Synchronize_subplots_with_dates.png")

Synchronize_subplots_with_dates

sorry but I just feel stupid trying to plot this, tried to use your approach on my data with ZoneDateTime (the original data), converting to DateTime (similar as your example). But my plots just look all the same without decently linked x-axis. In matlab this normally takes me 2 minutes :frowning: .

It is recommended to provide a MWE as per guidelines.

Sure, however I can’t upload attachments here. I’ve added a small dataset here: http://users.telenet.be/goosst/data.jld.
I saved this file in this way:
using JLD save("data.jld","timestamps",timestamps,"pressure",pressure,"timestamps2",timestamps2,"waterdepth",waterdepth)

thanks!

# plotting with zonedatetime
plot_water=plot(Dates.format.(timestamps2,"dd-HH:MM"),waterdepth, xrotation = 60, label="water depth",lw = 3)
plot_pressure=plot(Dates.format.(timestamps,"dd-HH:MM"),pressure,xrotation = 60,label="baro pressure", lw = 3,color=:red)
plot_merged=plot(plot_water,plot_pressure,layout = (2, 1),link=:x,xticks=10)


# using datetime instead of zonedatetime
timestamps_dt=DateTime[];
for i = 1:length(timestamps)
    push!(timestamps_dt,DateTime(timestamps[i]))
end

timestamps2_dt=DateTime[];
for i = 1:length(timestamps2)
    push!(timestamps2_dt,DateTime(timestamps2[i]))
end

plot_water=plot(Dates.format.(timestamps2_dt,"dd-HH:MM"),waterdepth, xrotation = 60, label="water depth",lw = 3)
Plots.plot!(xticks=(timestamps2_dt, Dates.format.(timestamps2_dt,"dd-HH:MM")))


plot_pressure=plot(Dates.format.(timestamps_dt,"dd-HH:MM"),pressure,xrotation = 60,label="baro pressure", lw = 3,color=:red)
Plots.plot!(xticks=(timestamps_dt, Dates.format.(timestamps_dt,"dd-HH:MM")))

plot_merged=plot(plot_water,plot_pressure,layout = (2, 1),link=:x,xticks=10)

Same workflow provided above seems to work on your real data after conversion from TimeZones to DateTime:

using TimeZones, Dates, Plots; gr()

# load data:
using JLD
d = load("data.jld")
pressure = d["pressure"]
dtm1 = Dates.datetime2epochms.(DateTime.(d["timestamps"]))
waterdepth = d["waterdepth"]
dtm2 = Dates.datetime2epochms.(DateTime.(d["timestamps2"]))

# process data:
dtmin, dtmax = minimum([dtm1[1];dtm2[1]]), maximum([dtm1[end];dtm2[end]])
dt_ticks = round.(Int64,LinRange(dtmin,dtmax,10))
str_ticks= Dates.format.(Dates.epochms2datetime.(dt_ticks),"dd-HH:MM")

# plot data:
pw = plot(dtm1,pressure, xrotation=60,label="water depth",c=:blue,lw=3,xticks=10)
plot!(xticks=(dt_ticks, str_ticks), ylims=(997,1015))

pp = plot(dtm2,waterdepth, xrotation=60,label="baro pressure",c=:red,lw=3,xticks=10)
plot!(xticks=(dt_ticks, str_ticks), ylims=(1.6,2.2))

plot_merged = plot(pw,pp, layout=(2,1), link=:x)   # optionally add: xlims=(dtmin,dtmax)

Synchronize_subplots_with_dates2

thanks a lot!

1 Like