How avoid this Warning when I use Contour

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

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

1 Like

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

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!

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

Not sure if this is what you want:

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

2 Likes

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 ().

So what was the result sought?

1 Like

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 to make it easier to help you.

2 Likes

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.

1 Like

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.

4 Likes

@MillerSilva, running your code above using Plots’ pyplot() backend results in:
pyplot_contour_2series_different_clevels

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

1 Like

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.

IntervalConstraintProgramming.jl (currently undergoing are rewrite)

1 Like

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
or like:
gr_contourf_intersection_different_clevels

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

1 Like

Can you show me your code please?

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

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

For the x-y plane region meeting last condition:

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

This other way displays the boundary too:

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])

4 Likes