# PGFPlotsX - How to save figure?

**URL:** <https://discourse.julialang.org/t/pgfplotsx-how-to-save-figure/44145>\
**Category:** New to Julia\
**Tags:** pgfplotsx\
**Created:** [August 2, 2020, 5:53pm UTC](https://discourse.julialang.org/t/pgfplotsx-how-to-save-figure/44145 "2020-08-02T17:53:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 2, 2020, 5:53pm UTC](https://discourse.julialang.org/t/pgfplotsx-how-to-save-figure/44145/1 "2020-08-02T17:53:49Z")

</div>

Hello!

I have some code as:

```julia
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:

```julia
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](https://discourse.julialang.org/t/an-exemple-in-order-to-save-figures-generated-by-pgfplotsx/38120) which shows that one can put it in some kind of “Axis” enviroment, but do I have to do that too?

```julia
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

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [August 2, 2020, 7:42pm UTC](https://discourse.julialang.org/t/pgfplotsx-how-to-save-figure/44145/2 "2020-08-02T19:42:45Z")

</div>

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

```julia
savefig(p, "Test.pdf")

```

should give what you are after

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 2, 2020, 7:52pm UTC](https://discourse.julialang.org/t/pgfplotsx-how-to-save-figure/44145/3 "2020-08-02T19:52:45Z")

</div>

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

```julia
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:

```julia
savefig(p,"TestNow.tex")

```

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/e/5ee6f4bc1000d2710f3e3723a25ec673bffc787c.png)

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

```julia
[/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;

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/2/429e4e49e03a851b868eed41b1c412c096eb326c.png)

It works as well.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [August 2, 2020, 8:26pm UTC](https://discourse.julialang.org/t/pgfplotsx-how-to-save-figure/44145/4 "2020-08-02T20:26:18Z")

</div>

> [@Ahmed\_Salih](#):
>
> but is it possible to export in a way so that it builds instantly?

For that you need to set `tex_output_standalone = true` as stated in the [documentation](http://docs.juliaplots.org/latest/backends/#%5BPGFPlotsX%5D(https://github.com/KristofferC/PGFPlotsX.jl)-1).

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 2, 2020, 8:31pm UTC](https://discourse.julialang.org/t/pgfplotsx-how-to-save-figure/44145/5 "2020-08-02T20:31:01Z")

</div>

Thanks!
