Showing shaded areas in StatPlots

Hi everyone. I have a time-series dataset which I’ve plotted in Julia using StatPlots.jl, and now would like to impose these grey bars on the plot to emphasize certain periods of time (such as wars/recessions etc.), I’ve attached an image below as an example, I’m using the same format (i.e. date on the x-axis, and some time-series measure on the y-axis). I’m trying to do so using the bar command. I’d like these grey areas to show up in periods 24-28, and periods 85-86 for example. I’ve attached my pseudo-code here:

plt1 = bar((24:28), (85-86), fill=:lightgrey, label= "War")

However, this plots only the y-values as such. I’ve also tried a few variations on bar_width (but not sure how to specify at which periods i.e. values on my x-axis I’d want them to be) and x=(24:28) but this returns the error that the y-value should be equal to the x-value or greater only by 1.

Would appreciate all help, thank you.

you seem to have forgotten to attach the image

just realised that, sorry! attached now.

There’s a vbox! command to add those

Is that in Plots.jl? I can’t find this in the docs or via Google (all results point to Makie instead).

The idea in the original post could be implemented like this, assuming you get your recession dates as start date and end date:

using TimeSeries, StatsPlots

dates = Date(2018, 1, 1):Day(1):Date(2018, 12, 31)

ta = TimeArray(dates, rand(length(dates)))

recessions = [(Date(2018, 4, 1), Date(2018, 5, 1))
              (Date(2018, 7, 1), Date(2018, 9, 1))]

function mid_length(x)
  length = x[2] - x[1]
  return length.value, x[1] + length/2
end

ml = mid_length.(recessions)

p = plot(ta)
for r in ml
  bar!(p, [r[2]], [1], width = r[1], color = "grey", alpha = 0.3, label = "")
end

display(p)

I seem to be off by a day or so so this can probably be refined:

Yeah sorry it’s called vspan, added in Infinite objects (fix #1422) by daschw · Pull Request #1445 · JuliaPlots/Plots.jl · GitHub . I mixed it up with the Makie term.

I have encountered some weird behaviour and it would be great, if someone could help me understand it.

using Plots, Dates

pyplot()
x   = Dates.Date(1960,1,1):Dates.Month(1):Dates.Date(2020,5,1)
plt = plot(x, randn(length(x)), alpha = 0.3, label = "")

recs = [Dates.Date(1960,04,1):Dates.Month(1):Dates.Date(1961,02,1),
        Dates.Date(1969,12,1):Dates.Month(1):Dates.Date(1970,11,1),
        Dates.Date(1973,11,1):Dates.Month(1):Dates.Date(1975,03,1),
        Dates.Date(1980,01,1):Dates.Month(1):Dates.Date(1980,07,1),
        Dates.Date(1981,07,1):Dates.Month(1):Dates.Date(1982,11,1),
        Dates.Date(1990,07,1):Dates.Month(1):Dates.Date(1991,03,1),
        Dates.Date(2001,03,1):Dates.Month(1):Dates.Date(2001,11,1),
        Dates.Date(2007,12,1):Dates.Month(1):Dates.Date(2009,06,1)]

for t in 1:8
    vspan!(plt, recs[t], label = "", color = :grey)
end

display(plt)

Which results in the following plot:

grafik

The following things seem to be weird: First, the grey bars look like that there are multiple ones that overlap, although it is just one recession that I specified. Second, the y-axis seems to be converted to something with a date-structure, but I did not intend that. Third, the bars are too thin and do not cover all of the period specified. What can I do about that? Is there a way to go about it using vspan?