Plot inside a function?

function f()
    Plots.plot(1:20)
end

function g()
    Plots.plot(1:20)
    return 1.0
end

in the above, calling f() would produce a plot, however, calling g() does NOT. Why? and how to fix it? thanks.

Here is why

function f()
    p = Plots.plot(1:20)
    return p
end

function g()
    Plots.plot(1:20)
    return 1.0
end

Function f() return the content of a plot while function g() returns a float64

1 Like

So does it mean that I have to return a Plot for a function to plot???

I mean, I want the function plots something (in the middle of a function) but return something else. Is it possible?

I see, the solution is to use display() like:

function g()
    display(Plots.plot(1:20) )
    return 1.0
end
2 Likes

Hello,
I am having the same problem. Your solution doesn’t work for me:

Thanks

This behaves differently in a Pluto notebook - the plot is shown only as a return value of a cell.
You can howver have plots in @info statements.