# Latex-friendly picture with Plots()

**URL:** https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223
**Category:** General Usage
**Created:** [November 18, 2019, 2:28pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223 "2019-11-18T14:28:38Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [November 18, 2019, 2:28pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/1 "2019-11-18T14:28:38Z")

</div>

Hi,

What is the best/simplest to use package to create pictures with Plots() that can be exported to latex? Trying to avoid the regular png.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [November 18, 2019, 2:34pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/2 "2019-11-18T14:34:23Z")

</div>

What is `Plots()`? Are you using the `Plots` package for plotting? If so, available output formats vary by backend, this overview should be helpful:

[https://docs.juliaplots.org/latest/output/](https://docs.juliaplots.org/latest/output/)

---

<div class="post-metadata">

### Author: ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)
#### Post date: [November 18, 2019, 2:50pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/3 "2019-11-18T14:50:38Z")

</div>

`Plots` is indeed a very good option, especially because it is also compatible with `PGFPlots` (LaTeX plotting) and thus it can save charts in .tex.

More complicated (but more flexible) packages include `PGFPlotsX`: [https://github.com/KristofferC/PGFPlotsX.jl](https://github.com/KristofferC/PGFPlotsX.jl) (it would be lovely to have it on `Plots` as a backend).

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 18, 2019, 3:07pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/4 "2019-11-18T15:07:47Z")

</div>

> [@fipelle](#):
>
> it would be lovely to have it on `Plots` as a backend

But then you would probably lose most of the power and flexibility, as Plots imposes a uniform frontend. Also, it already has a PGFPlots backend if you are just after the appearance and LaTeX compatibility.

---

<div class="post-metadata">

### Author: ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)
#### Post date: [November 18, 2019, 3:18pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/5 "2019-11-18T15:18:41Z")

</div>

> [@Tamas\_Papp](#):
>
> But then you would probably lose most of the power and flexibility, as Plots imposes a uniform frontend.

Fair enough, I think you are right - some functionalities could be added via recipes though.

> [@Tamas\_Papp](#):
>
> Also, it already has a PGFPlots backend if you are just after the appearance and LaTeX compatibility.

While this is technically true for most charts, the compatibility is not full. For instance, I remember that `PGFPlots` heatmaps were not fully supported in `Plots`.

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [November 18, 2019, 9:24pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/6 "2019-11-18T21:24:34Z")

</div>

How can I use PGFPlots and Plots together?

I tried  
using Plots  
pgfplots()

but this does not have the functionality of PGFPlots. For example, I cannot reproduce this simple example from the documentation of PGFPlots  
It says Linear not defined

```julia
x = [1,2,3]
y = [2,4,1]
Axis(Plots.Linear(x, y))

```

It says Linear not defined

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [November 18, 2019, 9:45pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/7 "2019-11-18T21:45:59Z")

</div>

Also, the link you sent me above does not show how to save figures in .tex

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [November 18, 2019, 9:59pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/8 "2019-11-18T21:59:30Z")

</div>

Plots.jl works with multiple plotting backends by transforming a standard input syntax into backend-appropriate calls. Examples for PGFPlots: [http://docs.juliaplots.org/latest/examples/pgfplots/](http://docs.juliaplots.org/latest/examples/pgfplots/)

The linked page doesn’t explicitly state how to get tex output, but you can infer from

> Command `savefig` chooses file type automatically based on the file extension.

> `savefig(fn) # save the most recent fig as fn`

that you should use

```julia
using Plots; pgfplots()
x, y = [1,2,3], [4,5,6]
plot(x,y)
savefig("path/to/directory/filename.tex")

```

which produces

```latex
\begin{tikzpicture}[]
\begin{axis}[height = {101.6mm}, ylabel = {}, xmin = {0.94}, xmax = {3.06}, ymax = {4.09}, xlabel = {}, unbounded coords=jump,scaled x ticks = false,xlabel style = {font = {\fontsize{11 pt}{14.3 pt}\selectfont}, color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0, rotate = 0.0},xmajorgrids = true,xtick = {1.0,1.5,2.0,2.5,3.0},xticklabels = {$1.0$,$1.5$,$2.0$,$2.5$,$3.0$},xtick align = inside,xticklabel style = {font = {\fontsize{8 pt}{10.4 pt}\selectfont}, color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0, rotate = 0.0},x grid style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 0.1,
line width = 0.5,
solid},axis x line* = left,x axis line style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 1.0,
line width = 1,
solid},scaled y ticks = false,ylabel style = {font = {\fontsize{11 pt}{14.3 pt}\selectfont}, color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0, rotate = 0.0},ymajorgrids = true,ytick = {1.0,2.0,3.0,4.0},yticklabels = {$1$,$2$,$3$,$4$},ytick align = inside,yticklabel style = {font = {\fontsize{8 pt}{10.4 pt}\selectfont}, color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0, rotate = 0.0},y grid style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 0.1,
line width = 0.5,
solid},axis y line* = left,y axis line style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 1.0,
line width = 1,
solid}, xshift = 0.0mm,
    yshift = 0.0mm,
    axis background/.style={fill={rgb,1:red,1.00000000;green,1.00000000;blue,1.00000000}}
,legend style = {color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000},
draw opacity = 1.0,
line width = 1,
solid,fill = {rgb,1:red,1.00000000;green,1.00000000;blue,1.00000000},font = {\fontsize{8 pt}{10.4 pt}\selectfont}},colorbar style={title=}, ymin = {0.91}, width = {152.4mm}]\addplot+ [color = {rgb,1:red,0.00000000;green,0.60560316;blue,0.97868012},
draw opacity = 1.0,
line width = 1,
solid,mark = none,
mark size = 2.0,
mark options = {
    color = {rgb,1:red,0.00000000;green,0.00000000;blue,0.00000000}, draw opacity = 1.0,
    fill = {rgb,1:red,0.00000000;green,0.60560316;blue,0.97868012}, fill opacity = 1.0,
    line width = 1,
    rotate = 0,
    solid
}]coordinates {
(1.0, 2.0)
(2.0, 4.0)
(3.0, 1.0)
};
\addlegendentry{y1}
\end{axis}

\end{tikzpicture}

```

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [November 18, 2019, 10:02pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/9 "2019-11-18T22:02:02Z")

</div>

> [@fipelle](#):
>
> (it would be lovely to have it on `Plots` as a backend).

It’s in the works: [https://github.com/JuliaPlots/Plots.jl/pull/2252](https://github.com/JuliaPlots/Plots.jl/pull/2252)

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [November 18, 2019, 11:54pm UTC](https://discourse.julialang.org/t/latex-friendly-picture-with-plots/31223/10 "2019-11-18T23:54:46Z")

</div>

thank you all for the reply so far.

I still have hard time adjusting to pgfplots(), compared to, say, pyplot(). I don’t know if this is the right place, but for example with pgfplots() this doesn’t work

```julia
aa = rand(10)
bb = rand(10)
conf = [aa bb]
p = plot(rand(10),rand(10))
plot!(rand(10),ribbon=conf)

```

And even if I replace ribbon with errorBars, it still doesn’t run.
