# Ghosting on contour plots of a (x,y)→Bool function

I would like to plot of a `(x,y) → Bool` function as a contour plot. For the function I am plotting I get a lot of ghosting. The closer to 45° the line is the better it looks. Compare the heatmap to the contour plots:

Do you have any tips on how I can get the contour plotting done reliably? Is there a better way to plot contours and boundaries? Ideally, I’d like to get a nice vector plots with lines on the boundaries.

The following seems to improve things:

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

boolxy(x,y) = (y>0.6-1e-3*x) & (y>5e-4*x-0.1) & (y>0.2) ? false : true

x, y = LinRange(0,900,500), LinRange(0,0.5,500)
plot(heatmap(x,y,boolxy,legend=false), contour(x,y,boolxy,levels=[0.01],legend=false), dpi=300)
``````

I have no clue how you found this combination. Inverting my function, like you do, helps somewhat. Adding the level at 0.01 makes the lines darker.

Still not perfect but sooo much better. Thanks @rafael.guerra

1 Like

@owiecc, the following workflow with a grain of ImageFiltering seems to be more robust, not requiring negating the boolean function, nor tuning the contour level between 0 and 1:

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

boolxy(x,y) = (y>0.6-1e-3*x) & (y>5e-4*x-0.1) & (y>0.2) ? true : false

x, y = LinRange(0,900,1000), LinRange(0,0.5,1000)
z = [boolxy(x,y) for y in y, x in x]

using ImageFiltering
kern1 = centered([1/3, 1/3, 1/3])
kernf = kernelfactors((kern1, kern1))
w = imfilter(z, kernf)
plot(heatmap(x,y,w), contour(x,y,w,levels=[0.5]), colorbar=false,dpi=300)
``````

1 Like

If the equations for the mask boundaries are available as in code above, then it might be preferable to plot them directly:

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

l1(x) = 0.6-1e-3*x
l2(x) = 5e-4*x-0.1
l3(x) = 0.2
bool1(x) = begin y=l1(x); (y>=l2(x)) & (y>=l3(x)) ? y : NaN; end
bool2(x) = begin y=l2(x); (y>=l1(x)) & (y>=l3(x)) ? y : NaN; end
bool3(x) = begin y=l3(x); (y>=l1(x)) & (y>=l2(x)) ? y : NaN; end

x = LinRange(0,900,100)
yy = @. [bool1(x); bool2(x); bool3(x)]
xx = [x; x; x]
zz = [xx yy]
nanr = isnan.(zz[:,2])
zz = zz[.!vec(nanr), :]
zz = sortslices(zz,dims=1,by=x->x)
plot(zz[:,1],zz[:,2]; lc=:black,legend=false,xlims=(0,900),ylims=(0,0.5), dpi=300)
``````

Unfortunately the function comes from some funky transformations so I don’t know the boundary equations. But I like the solution with the blur filter. I guess it makes it easier for the contour algorithm to trace the edges if the function is a bit smoother.

1 Like