# Latex-friendly picture with Plots()

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.

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/

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 (it would be lovely to have it on Plots as a backend).

3 Likes

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.

1 Like

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

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.

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

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


It says Linear not defined

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

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/

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

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


which produces

\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)
};
\end{axis}

\end{tikzpicture}


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

2 Likes

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

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.