# Plots.jl fill within limits only

**URL:** <https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852>\
**Category:** General Usage\
**Tags:** visualization\
**Created:** [April 8, 2021, 4:45pm UTC](https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852 "2021-04-08T16:45:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [April 8, 2021, 4:45pm UTC](https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852/1 "2021-04-08T16:45:08Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 8, 2021, 4:50pm UTC](https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852/2 "2021-04-08T16:50:43Z")

</div>

Maybe with contourf?

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [April 8, 2021, 5:40pm UTC](https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852/3 "2021-04-08T17:40:57Z")

</div>

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.  
 ![temp](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae3aa8450832d3f1f9b8a60614a141a5711b7b6d.png)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 8, 2021, 7:09pm UTC](https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852/4 "2021-04-08T19:09:22Z")

</div>

What happens if you use levels=[1, 2] or whichever levels you need?

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [April 8, 2021, 7:26pm UTC](https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852/5 "2021-04-08T19:26:25Z")

</div>

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

For example:

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

![image](https://global.discourse-cdn.com/julialang/original/3X/a/8/a8fba0cbda8ac69b66caa0fd95771a7bd1100cd1.jpeg)

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

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [April 8, 2021, 7:31pm UTC](https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852/6 "2021-04-08T19:31:36Z")

</div>

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

---

<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:** [April 8, 2021, 8:41pm UTC](https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852/7 "2021-04-08T20:41:41Z")

</div>

Another way is contour fusion:

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

```

![contour_fusion](https://global.discourse-cdn.com/julialang/original/3X/2/d/2d757d3df91a5c9d879ccf94ea10c012f2f71b6f.jpeg)

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [April 10, 2021, 8:27pm UTC](https://discourse.julialang.org/t/plots-jl-fill-within-limits-only/58852/8 "2021-04-10T20:27:05Z")

</div>

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