How do I save a plot?

julia> out = DataFrame(Q1 = [3,3,3], Q2 = [3,2,1])
3×2 DataFrame
│ Row │ Q1    │ Q2    │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 3     │ 3     │
│ 2   │ 3     │ 2     │
│ 3   │ 3     │ 1     │

julia> fig1 = pie([out.Q1], [1,2])

julia> fig1 = pie([out.Q1], [1,2,3])

julia> savefig(fig1)

So, I’m trying to save a pie chart from plots, created from a data frame.

When I do I get an error:

ERROR: MethodError: no method matching savefig(::Plots.Plot{Plots.GRBackend})

Is this to do with GRBackend, or do I need to add more details, like file type and location?

Check the function parameters, you are missing the filename. Use ?savefig in the REPL to get the help.

4 Likes

The second parameter is the output file location. The file type is deduced from the extension (png, svg…).

The error message gives you a hint:

ERROR: MethodError: no method matching savefig(::Plots.Plot{Plots.GRBackend})
Closest candidates are:
  savefig(::Plots.Plot, ::AbstractString) at /home/user/.julia/packages/Plots/qZHsp/src/output.jl:107
  ...
savefig(fig1,"C:/Users/brett/OneDrive/Documents/Soc323")

I did this, and did not get an error message, but no .png showed up in the destination folder.

Edit. I need to specify the file.

savefig(fig1,"C:/Users/brett/OneDrive/Documents/Soc323/fig1.png")

If you save several figures to the same directory, it is easier if you define a fig path:

figpath = "C:/Users/brett/OneDrive/Documents/Soc323/"
savefig(fig1,figpath*"fig1.png")
savefig(fig1,figpath*"fig1.svg")
savefig(fig1,figpath*"fig1.pdf")
# etc.
1 Like

Thanks, I figured it out and should have replied. Also, display(fig1), allows the figure to appear in a pdf.

help?> savefig
search: savefig StackOverflowError

  savefig([plot,] filename)

  Save a Plot (the current plot if plot is not passed) to file. The file type is inferred from the file extension.
1 Like

Would be great to eventually have an option to save plots and tables to a zip file or to a database or to send the plot and table to a queue like RabbitMQ.