Plotting and returning values from within a function

I am trying to call a plotting function and return a value from another function (plot_and_return_points(x,y)):

function plot_stuff(x, y)
plot(x, y)

function plot_and_return_points(x, y)
println(“The plot below won’t print, but the points will be returned”)
println(“How do I get the plot to print?”)
plot_stuff(x, y)
return x, y

This will return points but not plot
plot_and_return_points(1:10, rand(10))

The values x,y in the code above are returned, but the plot is not shown. I can call the plot_stuff function and the plot is shown, but I can not call the plot_and_return_points function to display the plot.

What am I missing?

You haven’t mentioned which plotting package you’re using, but if you’re using Plots.jl, try adding this after the call to plot


In the REPL, the plot is automatically shown if the plot is returned since the show method for plots is called automatically. If the plot is not returned from the function, you can instead manaully force the display. Sometimes it also works to call

plot(x, y, show=true)

and you can also do

fig = plot(x, y)
1 Like

Yes - I was using Plots.jl and this worked straight away.


1 Like