# How avoid this Warning when I use Contour

**URL:** <https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327>\
**Category:** General Usage\
**Tags:** plots\
**Created:** [May 17, 2021, 9:34pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327 "2021-05-17T21:34:03Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![MillerSilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/millersilva/32/25184_2.png) [@MillerSilva](https://discourse.julialang.org/u/MillerSilva)\
**Post date:** [May 17, 2021, 9:34pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/1 "2021-05-17T21:34:03Z")

</div>

Hello Julia comunity, I need your help.  
When I run this

> xs = range(-5,stop=7,length=50)  
> ys = range(-2,stop=12,length=50)  
> contour(xs,ys,h, levels=[0,2,3,20,30])  
> contour!(xs,ys,g, levels=[-7,-6,-5,-4,-3,-2,-1,-0.5,0])

It return this

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/1/31b0d92723f74b416fd3db14e093710268a0e4e1.png)

I would like to avoid that Warning, How can I do?

---

<div class="post-metadata">

**Author:** ![cadojo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cadojo/32/25328_2.png) [@cadojo](https://discourse.julialang.org/u/cadojo)\
**Post date:** [May 17, 2021, 9:48pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/2 "2021-05-17T21:48:37Z")

</div>

Does a `palette` keyword argument work here? I recall you being able to do…

```nohighlight
contour(xs, ys, h, levels=[0,2,3,20,30], palette = :blues)
contour!(xs, ys, g, levels=[-7,-6,-5,-4,-3,-2,-1,-0.5,0], palette = :reds)

```

There is a list of compatible color schemes for Plots.jl [here](https://docs.juliaplots.org/latest/generated/colorschemes/#ColorPalette)!

---

<div class="post-metadata">

**Author:** ![MillerSilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/millersilva/32/25184_2.png) [@MillerSilva](https://discourse.julialang.org/u/MillerSilva)\
**Post date:** [May 17, 2021, 10:00pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/3 "2021-05-17T22:00:06Z")

</div>

Thanks for replying but that couldn’t solve my problem. ☹

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/5/053c8efe1afa7540d9c324ad1f0107aa998e1023.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:** [May 17, 2021, 10:24pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/4 "2021-05-17T22:24:02Z")

</div>

Not sure if this is what you want:

```julia
using Colors, Plots;gr()

h(x,y) = y - x
g(x,y) = y - 2x
xs, ys = LinRange(-5,7,50), LinRange(-2,12,50)
lh, lg = [0,2,3,20,30], [-7,-6,-5,-4,-3,-2,-1,-0.5,0]
levels = [lh; lg]
c1, c2 = extrema(levels)
colors = distinguishable_colors(length(levels))
contour(xs,ys,h, levels=levels, clims=(c1,c2), c=colors)
contour!(xs,ys,g, levels=levels, clims=(c1,c2), c=colors)

```

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

---

<div class="post-metadata">

**Author:** ![MillerSilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/millersilva/32/25184_2.png) [@MillerSilva](https://discourse.julialang.org/u/MillerSilva)\
**Post date:** [May 18, 2021, 12:55am UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/5 "2021-05-18T00:55:16Z")

</div>

Thank you for your replying. Although it is not what I wanted, I learned something new. Now about my “Warning” problem, for some reason it disappeared when I used `pyplot ()`.

---

<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:** [May 18, 2021, 1:06am UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/6 "2021-05-18T01:06:49Z")

</div>

So what was the result sought?

---

<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:** [May 18, 2021, 4:05am UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/7 "2021-05-18T04:05:56Z")

</div>

Welcome to the Julia community!

Since we don’t know what your functions `h` and `g` are, we can’t run tests. You also didn’t tell us what you wanted to happen.

Please read [this explanation](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) to make it easier to help you.

---

<div class="post-metadata">

**Author:** ![MillerSilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/millersilva/32/25184_2.png) [@MillerSilva](https://discourse.julialang.org/u/MillerSilva)\
**Post date:** [May 18, 2021, 3:07pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/8 "2021-05-18T15:07:52Z")

</div>

I am sorry I did not put my problem in more detail, but I will try to make amends by explaining what I wanted.  
I was preparing a nonlinear programming class, and I needed to draw the feasible region of an example, using contour lines (it should be noted that I am a newbie to Julia). My problem was that when I compiled it I got a “Warning” message that I didn’t like, because it would look ugly when I presented it in class.  
My initial code was:

> using SymPy  
> using Plots  
> @vars x y  
> g(x,y)=x+0.2y-3  
> h(x,y)=5-x-y  
> xs = range(-5,stop=7,length=50)  
> ys = range(-2,stop=12,length=50)  
> contour(xs,ys,h, levels=[0,2,3,20,30])  
> contour!(xs,ys,g, levels=[-7,-6,-5,-4,-3,-2,-1,-0.5,0])

and that was returning a “Warning” as you can see above.

And my last code, which didn’t return any “Warning” was

> > using SymPy  
> > using Plots  
> > pyplot() #added  
> > @vars x y  
> > g(x,y)=x+0.2y-3  
> > h(x,y)=5-x-y  
> > xs = range(-5,stop=7,length=50)  
> > ys = range(-2,stop=12,length=50)  
> > contour(xs,ys,h, levels=[0,2,3,20,30])  
> > contour!(xs,ys,g, levels=[-7,-6,-5,-4,-3,-2,-1,-0.5,0])

I hope you understand what I mean. Thank you.

---

<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:** [May 18, 2021, 3:53pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/9 "2021-05-18T15:53:09Z")

</div>

I think what is not understood is what was the expected result of the plotting.

What the warning is telling (as far as I understand) is: Your first plot draws contours with a defined colormap,a. In this plot, the last color of the colorbar will correspond to the value 30, and the first color in the colorbar will correspond to 0. In the second plot, which uses the same color scheme, the first color of the colorbar will correspond to -7 and the last color will correspond to 0.

So in the end, the colors of the first plot would not correspond to the same values as the same colors in the second call to contour.

Now, what is that you wanted?  
Did you want a the colorbar to go from -7 to 30, and that both calls to contour used that scale (this is @rafael.guerra’s solution)? Or did you want to use two different colorbars to represent each of the calls?

So the warning is indeed useful, and the best way of avoiding it would depend on the sought result.

---

<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:** [May 18, 2021, 5:52pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/10 "2021-05-18T17:52:54Z")

</div>

@MillerSilva, running your code above using Plots’ pyplot() backend results in:  
 ![pyplot_contour_2series_different_clevels](https://global.discourse-cdn.com/julialang/original/3X/a/5/a56168241be390b2f1e8ea2b2d09a9ed5e87bccb.png)

The color bar seems to be a complete shambles and contours missing.  
How does yours look like?

---

<div class="post-metadata">

**Author:** ![MillerSilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/millersilva/32/25184_2.png) [@MillerSilva](https://discourse.julialang.org/u/MillerSilva)\
**Post date:** [May 26, 2021, 12:46am UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/11 "2021-05-26T00:46:10Z")

</div>

Sorry for taking time to reply, I was very busy.  
What he wanted was the following.  
I wanted a tool to draw feasible regions. For example if we have two functions f (x, y) and g (x, y), then how to graph the intersection of the regions f (x, y) \<10 and g (x, y) \<20. I started looking and found the contour tool, with which I could graph the region of R ^ 2 where f (x, y) = 9, f (x, y) = 8, … and so on in such a way as to is making a sketch of the region that I want. And then analogously with g. I was liking the way it looked but I didn’t like Warning’s message, which I now know was really useful, thanks to one of you.  
I still can’t find the tool I’m telling you.  
But I have improved a bit in the use of contour.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/8/f8a32a963cc1b2b048def5573c27e0176ec08662.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:** [May 26, 2021, 6:45am UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/12 "2021-05-26T06:45:10Z")

</div>

IntervalConstraintProgramming.jl (currently undergoing are rewrite)

---

<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:** [May 26, 2021, 7:02am UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/13 "2021-05-26T07:02:23Z")

</div>

It seems to be better to have a proper software warning than a poor plot?  
Living with the warning we can get plots for the intersection of `f(x,y)<10` with `g(x,y)<20`, like:  
 ![gr_contour_intersection_different_clevels](https://global.discourse-cdn.com/julialang/original/3X/a/a/aad65c0c1d76867f3a60e7257207ba711766d0bf.png)  
or like:  
 ![gr_contourf_intersection_different_clevels](https://global.discourse-cdn.com/julialang/original/3X/b/3/b3979b477657d3f750e0f411d0f719694d449d95.png)

**NB:**  
_The zero contour level is an abstraction to indicate the intersection of the functions’ surfaces themselves._

---

<div class="post-metadata">

**Author:** ![MillerSilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/millersilva/32/25184_2.png) [@MillerSilva](https://discourse.julialang.org/u/MillerSilva)\
**Post date:** [May 26, 2021, 9:45pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/14 "2021-05-26T21:45:50Z")

</div>

> [@rafael.guerra](#):
>
> It seems to be better to have a proper software warning than a poor plot?  
> Living with the warning we can get plots for the intersection of `f(x,y)<10` with `g(x,y)<20` , like:

Can you show me your code please?

---

<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:** [May 26, 2021, 10:54pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/15 "2021-05-26T22:54:21Z")

</div>

@MillerSilva, sure but is the type of intersection posted the result you are looking for?

---

<div class="post-metadata">

**Author:** ![MillerSilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/millersilva/32/25184_2.png) [@MillerSilva](https://discourse.julialang.org/u/MillerSilva)\
**Post date:** [May 26, 2021, 11:41pm UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/16 "2021-05-26T23:41:17Z")

</div>

Not exactly, I would like the region of the intersection to be completely painted.

---

<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:** [May 27, 2021, 12:08am UTC](https://discourse.julialang.org/t/how-avoid-this-warning-when-i-use-contour/61327/18 "2021-05-27T00:08:40Z")

</div>

For the x-y plane region meeting last condition:

```julia
using Plots;gr()

f(x,y) = x + y - 2
g(x,y) = x - y - 1

θ(x,y) = (f(x,y) < 10) & (g(x,y) < 20 ) ? 1 : NaN

x = y = LinRange(-30,30,1000)
heatmap(x,y,θ, c=:lightblue, legend=false, ratio=1, xlims=extrema(x),ylims=extrema(y))

```

 ![intersection_001](https://global.discourse-cdn.com/julialang/original/3X/0/5/05e6c2f376ba1d0caae22ffefb9cfbdaab6296b9.png)

This other way displays the boundary too:

```julia
using Colors, Plots;gr()

f(x,y) = x + y - 2
g(x,y) = x - y - 1

θ(x,y) = (f(x,y) < 10) & (g(x,y) < 20 ) ? 1 : -1

x = y = LinRange(-30,30,1000)
Plots.contourf(x,y,θ, c = palette([:white,:lightblue]), legend=false,ratio=1, xlims=extrema(x),ylims=extrema(y))
Plots.contour!(x,y,θ, c=:red, lw=2,levels=[0])

```

 ![intersection2](https://global.discourse-cdn.com/julialang/original/3X/3/5/356786d9162ccb05efc1b40db57a2a293d347e0c.png)
