1

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.

aight101
  • 63
  • 6

1 Answers1

0

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()
Elias Carvalho
  • 286
  • 1
  • 5
  • I first get a 'plot' not defined error -- changing it to 'Plots.plot(x,y1)' fixes this. Then running it i recieve a 'MethodError: no method matching savefig(::Plots.Plot{Plots.GRBackend}, ::String)' – aight101 Jun 15 '22 at 19:42
  • Your example works fine on Julia 1.7 and the latest Plots/GR version. Have you tried restarting Julia and running the code that Elias posted? – Nils Gudat Jun 16 '22 at 13:30