Save Plot figure in Julia

Viewed 158

I am new to Julia and am using the Plots.jl [GR] library to create plots. I am trying to save my plot as a PNG to display on my GTK GUI. I am able to save the plot as an html file and text file but not .png using the savefig function (https://docs.juliaplots.org/latest/api/#Plots.savefig-Tuple{Plots.Plot,%20AbstractString})

I have been unsuccessful following this post: https://stackoverflow.com/a/62981826/13306172

The error i receive is:

ERROR: MethodError: no method matching savefig(::Plots.Plot{Plots.GRBackend}, ::String)
 x = 1:10; y = rand(10, 4)
import GR
using Plots
using PlotlyBase
using PlotlySave
gr()
import PlotlySave.savefig

function return_image_plot()
        x = 1:10; y = rand(10, 4) 
        x1 = 1:10; y1 = rand(10, 1) 
        plot1  = Plots.plot(x, y)
        plot2  = Plots.plot(x1, y1)
        plotty = Plots.plot(plot1, plot2, layout = (2, 1))
        
        Plots.savefig(plotty, "ploty2.png")
end

I also attempted to use I also attempted to use PlotySave Julia library https://github.com/hhaensel/PlotlySave.jl but was unsuccessful and also received a 'no matching method' error

I have followed/copied multiple SoF posts but have continued to be unsuccessful. Any help is appreciated.

1 Answers

Try this:

using Plots

function saveplot()
    x = 1:10
    y1 = rand(10, 4)
    y2 = rand(10, 1)
    p1 = plot(x, y1)
    p2 = plot(x, y2)
    p = plot(p1, p2, layout=(2, 1))
    
    savefig(p, "plot.png")
end

saveplot()
Related