PGFPlotsX - How to save figure?


I have some code as:

p = scatter((data[1,id1],data[2,id1]),
            markersize = 3.5,
            markerstrokestyle = :dot,
            color = :orange,
            label = "Sick"

            markersize = 3.5,
            markerstrokestyle = :dot,
            color = :black,
            label = "Healthy"


Where I am using Plots with a PGFPlotsX backend to construct a plot as usual. When I try to save using “pgfsave” it errors out. Would anyone know how to fix this? A simple example is:

x = 1
y = 1
p = plot(x,y)

Would anyone know how to fix this? I am aware of An exemple in order to save figures generated by PGFPlotsX which shows that one can put it in some kind of “Axis” enviroment, but do I have to do that too?

ERROR: MethodError: no method matching save(::String, ::Plots.Plot{Plots.PGFPlotsXBackend})
Closest candidates are:
  save(::AbstractString, ::Plot; kwargs...) at C:\Users\Ahmed Salih\.julia\packages\PGFPlotsX\rS8LC\src\axiselements.jl:617
  save(::AbstractString, ::PGFPlotsX.AxisLike; kwargs...) at C:\Users\Ahmed Salih\.julia\packages\PGFPlotsX\rS8LC\src\axislike.jl:45
  save(::AbstractString, ::TikzPicture; kwargs...) at C:\Users\Ahmed Salih\.julia\packages\PGFPlotsX\rS8LC\src\tikzpicture.jl:34
 [1] top-level scope at REPL[12]:1

Kind regards

The function to save plots is called savefig in Plots. So

savefig(p, "Test.pdf")

should give what you are after

1 Like

Oh wauw, my mistake! I couldn’t get it to work, so I ended up doing it the PGFPlotsX way…

p = @pgf Axis(
        legend_pos = "outer north east",
        xlabel = "distance",
        ylabel = "track time",
            "scatter/classes" = {
                0 = {mark = "square*", "blue"},
                1 = {mark = "triangle*", "red"},
                #c = {mark = "o", draw = "black"},
                "only marks",
                "scatter src" = "explicit symbolic",
                    meta = "label"
                x = data[1,:],
                y = data[2,:],
                label = z,
        Legend(["Healthy", "Sick"])
    display("image/png", p)

I see that I can export is as “tex” also, but is it possible to export in a way so that it builds instantly? I tried to do it by saying:


But to actually get it to work in LaTeX I had to do:

Everything in yellow is what I added. I also had to remove one line:

[/tikz/background rectangle/.style={fill={rgb,1:red,1.0;green,1.0;blue,1.0}, draw opacity={1.0}}, show background rectangle]

Since it could not understand that line.

Kind regards

EDIT: Just wanted to say that I really liked the PGFPlotsX way of doing things, it felt much more natural than using Plots natively in my experience - nice that one can do both though!

EDIT2: If I just remove this part of the line;

It works as well.

For that you need to set tex_output_standalone = true as stated in the documentation.