# Julia plots: How to fill graph background with different colors

**URL:** <https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040>\
**Category:** General Usage\
**Tags:** package, plotting\
**Created:** [November 1, 2018, 7:41am UTC](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040 "2018-11-01T07:41:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pythonuser](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pythonuser](https://discourse.julialang.org/u/pythonuser)\
**Post date:** [November 1, 2018, 7:41am UTC](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040/1 "2018-11-01T07:41:51Z")

</div>

I am trying make a plot with the background colors different for each section of the y-axis. For example:

```julia
using Plots
gr()
plot(Plots.fakedata(50, 1), w=3, fill = (0, 0.2, :red))

```

generates the image:

 ![example](https://global.discourse-cdn.com/julialang/original/3X/5/0/5046b44fa683cfbb310baab201f2a4b41e940218.png)  
I want to fill the background of the graph with different colors. Like the area from -2 to 0 might be in red and be labelled as “risky”, from 0 to 2 as green and labelled as “ok”, etc.  
Something like this:

```julia
plot(Plots.fakedata(50, 2), w=3)
hline!( [collect(-10:10)], color=[:red,:green], width=30, alpha=collect(0.1:0.1:0.2), legend=false)

```

which generates this:

 ![example2](https://global.discourse-cdn.com/julialang/original/3X/8/6/8644e7e717480bcdc5e9246e540cc3eb43dae6cb.png)

I looked at the plots documentation and could not figure a way to do it. Does the fill keyword argument accept a list of fillrange, fillalpha and fillcolor? is there any other way to do it?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 4, 2018, 11:05am UTC](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040/2 "2018-11-04T11:05:14Z")

</div>

Do you mean something like this?

```julia
using Plots
gr()
data = Plots.fakedata(50,2)
p = plot(data, w=3);
hspan!(p,[0,maximum(data)], color = :green, alpha = 0.2, labels = "good");
hspan!(p,[minimum(data),0], color = :red, alpha = 0.2, labels = "bad");
display(p) # or savefig(p, "filename.png")

```

![display](https://global.discourse-cdn.com/julialang/original/3X/c/f/cf14b248de389b4cc0d1f0bd4f36dac3bfffc765.png)

I’ve also tried combining the two calls to `hspan!`, but the backend didn’t like that on my pc - might work on yours.

---

<div class="post-metadata">

**Author:** ![pythonuser](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pythonuser](https://discourse.julialang.org/u/pythonuser)\
**Post date:** [November 12, 2018, 3:19am UTC](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040/3 "2018-11-12T03:19:59Z")

</div>

> [@Sukera](#):
>
> savefig(p, “filename.png”)

Thanks @Sukera. This is exactly what I was trying to do!

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 12, 2018, 7:50am UTC](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040/4 "2018-11-12T07:50:56Z")

</div>

That’s great! If my post solved your problem, please mark it as the solution so other people know that your question has been answered 🙂

---

<div class="post-metadata">

**Author:** ![dalarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dalarev/32/9881_2.png) [@dalarev](https://discourse.julialang.org/u/dalarev)\
**Post date:** [March 26, 2021, 7:36pm UTC](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040/5 "2021-03-26T19:36:15Z")

</div>

Is it possible to apply a gradient rather than a static color array to the background fill?

It’s straightforward to apply it to a curve/scatter plot, but I can’t get the background gradient to work (using Plots).

---

<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:** [March 26, 2021, 8:52pm UTC](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040/6 "2021-03-26T20:52:04Z")

</div>

Check this past [post](https://discourse.julialang.org/t/can-i-paint-the-background-of-a-plot/) for a beautiful solution by @klaff using Plots, Colors and PolygonOps.

---

<div class="post-metadata">

**Author:** ![dalarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dalarev/32/9881_2.png) [@dalarev](https://discourse.julialang.org/u/dalarev)\
**Post date:** [March 26, 2021, 9:54pm UTC](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040/7 "2021-03-26T21:54:09Z")

</div>

Thanks! I’ll have to take some time to understand that code.

I think I found `fill` might actually work better for my application: reflectance spectrum as a function of wavelength in the visible range.

![a](https://global.discourse-cdn.com/julialang/original/3X/a/6/a6e985065cc25dec2d0e63c1b4e5aa22257cd4f7.png)

---

<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:** [July 11, 2021, 9:11pm UTC](https://discourse.julialang.org/t/julia-plots-how-to-fill-graph-background-with-different-colors/17040/8 "2021-07-11T21:11:53Z")

</div>

In reviewing this post, it appears that the following code more directly answers the OP’s question:

 ![plot_gr_threshold_filled_areas](https://global.discourse-cdn.com/julialang/original/3X/7/7/77629d643773c07c0626be6aedef66b166605161.png)

_ **NB:** Congratulazioni Italia!_

```julia
using Plots; gr()
theme(:dark)

x = LinRange(0,π,2021); y = 2*cos.(x)
ymin, ymax = extrema(y)
c = [-1.; 1.] # BAD < -1 ; -1 < OK < 1; GOOD > 1
cs = @. (c - ymin)/(ymax - ymin) # map input data ranges to color scale 0-1 range:
my_cgrad2 = cgrad([:red,:white,:green], cs, categorical=true)

scatter([-1 -1 -1],[0 0 0], mc=[:red :white :green],label=["BAD < -1" "-1 < OK < 1" "GOOD > 1"],marker=:rect, ratio=1)
plot!(x,y, fill=true,fill_z=y,fc=my_cgrad2, colorbar=false,clims=(ymin,ymax),lc=:black,
     xlabel="x", ylabel="y", widen=false,label=false, xlims=extrema(x),ylims=(-2.1,2.1))
plot!(xticks=([0, pi/3, 2pi/3, pi], ["0","π/3","2π/3", "π"]), dpi=300)

```
