# Plots.jl fill within limits only

Quick question. Is it possible in plots to only fill a region greater than some contour level? I’m trying to blank out part of the plot and leave the rest visible underneath.

``````x = 1:0.5:20
y = 1:0.5:10
contour(x, y, (x,y)->sin(x)+cos(y)) # background
contour!(x, y, (x,y)->x^2+y^2,clims=(0,50),fill=(true,:grays)) # bottom left quarter circle
``````

Maybe with contourf?

Do you have any idea how I would get that to work? Using `contourf!(x, y, (x,y)->x^2+y^2,clims=(0,50))` gives this image which wipes out the first contour plot entirely. What happens if you use levels=[1, 2] or whichever levels you need?

Some combination of NaN (which are ignored when plotting) and background_color=:transparent could help.

For example:

``````julia> using Plots
julia> x = 1:0.5:20
1.0:0.5:20.0
julia> y = 1:0.5:10
1.0:0.5:10.0
julia> contour(x, y, (x,y)->50(sin(x)+cos(y)))
julia> contour!(x, y, (x,y) -> (x^2+y^2 > 50) ?  NaN :  x^2+y^2,background_color = :transparent, fill = true)
``````

Yields: Which looks weird but at least they all show up. (I multiplied the first one because both calls to contour reuse the colorbar).

4 Likes

I wonder if you could build a new color scheme and set alpha = 0 for the last value, making everything above it transparent.

1 Like

Another way is contour fusion:

``````using Plots; gr()

f(x,y) = sin(x)+cos(y)
g(x,y) = x^2+y^2

fusion(x,y; cl=50) = (g(x,y) > cl) ? f(x,y) : g(x,y)

x = 1:0.025:20
y = 1:0.025:10
z = [fusion(x,y) for x in x, y in y];
contourf(x, y, fusion, levels= (2:4:50))
contour!(x, y, fusion, levels= union((-2:0.2:2),(2:4:50)), c=:black)
`````` 4 Likes

This seems like the approach with the most control. Thanks!