# Delete/redraw plot in Makie

I have a neural network and I am plotting its decision boundary using a trick with `contourf!` (basically make a 2D grid of points then apply the neural network to classify each point; contour line will end up on the decision boundary since there are only two levels). Now I want to do this plot every so often so I can see the boundary update as I train the neural network.

One thing I have considered is to make a `Node` with my neural network inside, but it really just seems easier to delete the old contour plot and re-plot it. Is there a way to do this in Makie?

Here’s the relevant plotting code:

``````function decisionboundary!(axis, m; f = x -> x > 0 ? 1 : 0, npts = 200, fill = false, pltargs...)
xmin = 0.9 * minimum(axis.limits[])
xmax = 0.9 * maximum(axis.limits[])
ymin = 0.9 * minimum(axis.limits[])
ymax = 0.9 * maximum(axis.limits[])
Δx = (xmax - xmin) / npts
Δy = (ymax - ymin) / npts
x = range(xmin, step = Δx, length = npts)
y = range(ymin, step = Δy, length = npts)
features = vcat(repeat(reshape(x, 1, :), inner = (1, npts)), repeat(reshape(y, 1, :), outer = (1, npts)))
Y = _applymodel(m, f, features, npts) # my attempt to lift the neural network

if all(Y .== first(Y))
@warn "Not drawing decision boundary because there is only one resulting class"
return nothing
end

return fill ? contourf!(axis, vec(x), vec(y), Y; linewidth = 2, levels = 1, pltargs...) :
contour!(axis, vec(x), vec(y), Y; linewidth = 2, levels = 1, pltargs...)
end

function _applymodel(m, f, features, npts)
Y = m(features)
if size(Y, 1) == 1
Y = f.(Y)
else
Y = f.(eachcol(Y))
end
Y = permutedims(reshape(Y, npts, npts))

return Y
end
_applymodel(m::Node, f, features, npts) = @lift(_applymodel(\$m, f, features, npts))
``````

Wow this actually just worked! All I did was

``````modeltrace = Node(m) # m is my neural network
decisionboundary!(axis, m)
``````

then call `modeltrace[] = m` each time I want an update.

Makie is so awesome! Thank you @sdanisch and @jules for such an amazing package.

2 Likes