Julia plots: How to fill graph background with different colors



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

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

generates the image:

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:

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:

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?


Do you mean something like this?

using Plots
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")


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


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


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 :slight_smile: