So, to keep things on a plot (like Matlab’s “hold on”), you call plot as plot!(…).
If I make a function such as
function plotsomething(input)
intermediate result = do something with input
plot(intermediate result)
end
Is there an easy way to call plotsomething!() instead of plotsomething() and have it apply the “hold on” properties to the plot I used in the function?
There are two things I can think of, maybe one of them can fit your needs. The first is to write some separate function f(input) = # do something with input, and then you can have
function plotsomething(input)
intermediate_result = f(input)
plot(intermediate_result)
end
and
function plotsomething!(input)
intermediate_result = f(input)
plot!(intermediate_result)
end
(the definition of f is just to save on lines of code). The other possibility is something like
function plotsomething(input, hold_on = true)
intermediate_result = f(input)
if hold_on
plot!(intermediate_result)
else
plot(intermediate_result)
end
You can also elect to make an empty plot with plt = plot() ahead of time and pass it into the function plotsomething, and then add what you want from the function with plot(plt, ...) where the ellipses indicate what you would put in plot! if you were simply adding to the last edited plot.
Hi Simon, does the callback solution allow handling independently different plot objects p1, p2, … ?
I get the following error:
p2 = plotsomething([-10,2]; ms=8, st=:scatter)
plotsomething([2,2]; pf=p2, ms=8, st=:scatter)
ERROR: MethodError: objects of type Plots.Plot{Plots.GRBackend} are not callable