PGFPlotsX - How to save figure?

Hello!

I have some code as:

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

    scatter!((data[1,id2],data[2,id2]),
            markersize = 3.5,
            markerstrokestyle = :dot,
            aspect_ratio=:equal,
            color = :black,
            label = "Healthy"
            );

    display(p);
    pgfsave("Test.pdf",p)

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)
pgfsave("Test.pdf",p)

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
  ...
Stacktrace:
 [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"},
            }
        },
        Plot(
            {
                scatter,
                "only marks",
                "scatter src" = "explicit symbolic",
            },
            Table(
                {
                    meta = "label"
                },
                x = data[1,:],
                y = data[2,:],
                label = z,
            )
        ),
        Legend(["Healthy", "Sick"])
    )
    #display(p)
    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:

savefig(p,"TestNow.tex")

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.

2 Likes

Thanks!