Plotting points on a plot with subplots

I have a rather long time series. For example, T = mx100. I can break this long series into m subplots. For example:

using Plots

# number of supblots

# Size of my time series
T = m*100

# Time
t_ = 1:T
t_ = reshape( t_ , 100,m)

# X time series
X = rand(Normal(0,1), T)
X = reshape( X, 100,m)

# Plot
plot(t_, X,  fmt=:png ,  layout = (m,1))

I have a vector U of size T of many zeros and few ones. For example:

# Points
U = rand(Binomial(1,.3) , T  )
U = [1:T U ]

I want to add to the previous plot, points just on the time axis where the 1’s in the U vector happen. If for example I had two ones at position 25 and 75, I can do this (here m=5) :

# Plot
plot(t_, X,  fmt=:png ,  layout = (m,1), legend = false)
scatter!([25, 75 ], [0 , 0],color=:red, markersize=4)

Unfortunately, I cannot generalize to the general U case.

A simple solution using the same approach, is to convert the 0s to NaN, so as to ignore them in scatter():

U = rand(Binomial(1,.3) , T);
U2_ = convert.(Float64, U);
U2_ = reshape(U2_, 100, m);
U2_[U2_ .== 0] .= NaN;
scatter!(t_, U2_, c=:red, ms=4)
1 Like