# Linking subplot x-axes containing dates

**URL:** <https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004>\
**Category:** New to Julia\
**Tags:** dates, plots\
**Created:** [May 12, 2021, 7:55am UTC](https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004 "2021-05-12T07:55:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![goosst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goosst/32/19090_2.png) [@goosst](https://discourse.julialang.org/u/goosst)\
**Post date:** [May 12, 2021, 7:55am UTC](https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004/1 "2021-05-12T07:55:57Z")

</div>

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:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/6/e/6e9cc19300a20c6f216248e668d7332d7b5d7be7.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/e/8ecb31f72382847fc60758b02d920d80824b17f1.png)

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

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

```

![image](https://global.discourse-cdn.com/julialang/original/3X/1/7/17bf93dd3ab70e16e5062bd8890e1536e819957f.png)

What essential part am I missing?

thanks!

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

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 12, 2021, 3:34pm UTC](https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004/2 "2021-05-12T15:34:01Z")

</div>

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?

---

<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 12, 2021, 4:44pm UTC](https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004/3 "2021-05-12T16:44:57Z")

</div>

Did you try `xlims`? See proper syntax for dates [here](https://discourse.julialang.org/t/plots-xlims-with-x-axis-given-by-dates/47926/2).

---

<div class="post-metadata">

**Author:** ![goosst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goosst/32/19090_2.png) [@goosst](https://discourse.julialang.org/u/goosst)\
**Post date:** [May 16, 2021, 7:07am UTC](https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004/4 "2021-05-16T07:07:51Z")

</div>

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](https://discourse.julialang.org/t/plots-xlims-with-x-axis-given-by-dates/47926/2).

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

---

<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 16, 2021, 9:08am UTC](https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004/5 "2021-05-16T09:08:33Z")

</div>

It is recommended to provide a MWE as per [guidelines](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757).

The following seems to work:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/3/a/3a53f4730556afcabf10b342e40ed6f4052ba63b.png)

---

<div class="post-metadata">

**Author:** ![goosst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goosst/32/19090_2.png) [@goosst](https://discourse.julialang.org/u/goosst)\
**Post date:** [May 17, 2021, 6:23pm UTC](https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004/6 "2021-05-17T18:23:07Z")

</div>

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 ☹ .

> It is recommended to provide a MWE as per [guidelines](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757).

Sure, however I can’t upload attachments here. I’ve added a small dataset here: [http://users.telenet.be/goosst/data.jld](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!

```julia
# 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)

```

---

<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 17, 2021, 7:48pm UTC](https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004/7 "2021-05-17T19:48:19Z")

</div>

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

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/8/1/81767ed0b76814468d5373b4d3fd9c79a1122e84.png)

---

<div class="post-metadata">

**Author:** ![goosst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goosst/32/19090_2.png) [@goosst](https://discourse.julialang.org/u/goosst)\
**Post date:** [May 18, 2021, 6:05pm UTC](https://discourse.julialang.org/t/linking-subplot-x-axes-containing-dates/61004/8 "2021-05-18T18:05:12Z")

</div>

thanks a lot!
