# Showing shaded areas in StatPlots

**URL:** <https://discourse.julialang.org/t/showing-shaded-areas-in-statplots/24803>\
**Category:** Visualization\
**Created:** [May 31, 2019, 3:18pm UTC](https://discourse.julialang.org/t/showing-shaded-areas-in-statplots/24803 "2019-05-31T15:18:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)\
**Post date:** [May 31, 2019, 3:18pm UTC](https://discourse.julialang.org/t/showing-shaded-areas-in-statplots/24803/1 "2019-05-31T15:18:49Z")

</div>

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:

```julia
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.

 ![NBERrec](https://global.discourse-cdn.com/julialang/original/3X/c/0/c09e226197780f05fdf2fecebdc4ed175c6a1404.gif)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 31, 2019, 3:24pm UTC](https://discourse.julialang.org/t/showing-shaded-areas-in-statplots/24803/2 "2019-05-31T15:24:48Z")

</div>

you seem to have forgotten to attach the image

---

<div class="post-metadata">

**Author:** ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)\
**Post date:** [May 31, 2019, 3:25pm UTC](https://discourse.julialang.org/t/showing-shaded-areas-in-statplots/24803/3 "2019-05-31T15:25:37Z")

</div>

just realised that, sorry! attached now.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 31, 2019, 7:58pm UTC](https://discourse.julialang.org/t/showing-shaded-areas-in-statplots/24803/4 "2019-05-31T19:58:48Z")

</div>

There’s a `vbox!` command to add those

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 31, 2019, 8:57pm UTC](https://discourse.julialang.org/t/showing-shaded-areas-in-statplots/24803/5 "2019-05-31T20:57:08Z")

</div>

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:

```julia
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:

 ![recessions](https://global.discourse-cdn.com/julialang/original/3X/e/2/e2ada941d38325e707234ae8244bd2178d7f97e4.jpeg)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 31, 2019, 9:17pm UTC](https://discourse.julialang.org/t/showing-shaded-areas-in-statplots/24803/6 "2019-05-31T21:17:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [May 8, 2020, 7:40am UTC](https://discourse.julialang.org/t/showing-shaded-areas-in-statplots/24803/7 "2020-05-08T07:40:16Z")

</div>

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

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

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`?
