# Gnuplot and TiKZ: centering plots?

I'm trying to plot an implicit function in a LaTeX document, and I'm using TiKZ for all my diagrams. For the implicit function, I'm calling gnuplot, and I've found that the best solution, which seems to be portable across my different systems, is to use the gnuplottex package, and set the gnuplot terminal to be epslatex. I've tried using \draw[raw gnuplot] in TiKZ, and I've experimented with pgfplots; but none of these gives me a correct plot. (Well, in fact raw gnuplot gives a wonderful result on one of my systems, and a messy job with crazy lines everywhere on another.)

However! even though the gnuplot output is in itself correct, I actually want it in a larger TIKZ diagram, so I need to somehow ensure that gnuplot's (0,0) coincides with TiKZ's, when I treat the gnuplot picture as a TiKZ node:

\begin{tikzpicture}
\draw[color=lightgray] (-4,0) -- (4,0);
\draw[color=lightgray] (0,-4) -- (0,4);
\draw[dashed,color=lightgray] (-1,-1) rectangle (1,1);
\node[scale=1.2] at (0,0) {
\begin{gnuplot}[terminal=epslatex]
set contour
set cntrparam levels incremental -0.02,0.01,0.02
set view map
set view equal
unset surface
unset key
unset tics
unset border
set isosamples 1000,1000
set xrange [-3.5:3.5]
set yrange [-3.5:3.5]
set view 0,0
set cont base
splot x**3 + y**3 - 6*x*y
\end{gnuplot}
};
\end{tikzpicture}


At least I'd like the axes and a few other things drawn with TiKZ, and then I can position the gnuplot graph correctly on the diagram.

However, currently the gnuplot (0,0) is somewhat to the right and above TiKZ's (0,0). I know I can use the TiKZ grid command and reposition the axes so that they are in the right place, but that seems a bit fiddly.

Is there a simple, elegant, portable way of lining up TiKZ and a gnuplot plot?

Note: For a complete example, see epslatextest.tex and epslatextest.pdf.

\documentclass{article}

\usepackage{tikz}
\usepackage[shell]{gnuplottex}

\begin{document}
The difficulty is shown in the figure.

\begin{figure}[h]%
\centering%
\begin{tikzpicture}
\begin{scope}
\draw[color=lightgray] (-4,0) -- (4,0);
\draw[color=lightgray] (0,-4) -- (0,4);
\draw[dashed,color=lightgray] (-1,-1) rectangle (1,1);
\end{scope}

\node[scale=1.2] at (0,0) {
\begin{gnuplot}[terminal=epslatex]
set contour
set cntrparam levels incremental -0.02,0.01,0.02
set view map
set view equal
unset surface
unset key
unset tics
unset border
set isosamples 1000,1000
set xrange [-3.5:3.5]
set yrange [-3.5:3.5]
set view 0,0
set cont base
splot x**3 + y**3 - 6*x*y
\end{gnuplot}
};
\end{tikzpicture}

\caption{A test of gnuplottex with epslatex}
\label{fig:gnuplot}
\end{figure}
\end{document}

• Can you turn the code snippet into a full compilable example? – egreg Jun 16 '14 at 7:58

I don't know if this is the best(tm) way, but it works :)

You can use the tikz terminal, and give it the size you want. For this you must set the plot size equal to the canvas size (set lmargin ... etc.). (I haven't tried with the plotsize option).

To overlap your picture with the gnuplot tikzpicture environment, you can shift the scope.

\documentclass{article}

\usepackage{tikz}
\usepackage{gnuplot-lua-tikz}
\usepackage[shell]{gnuplottex}
\thispagestyle{empty}

\begin{document}
The difficulty is shown in the figure.

\begin{figure}[h]%
\centering%
\begin{tikzpicture}
\begin{scope}[shift={(4,4)}]
\draw[color=lightgray] (-4,0) -- (4,0);
\draw[color=lightgray] (0,-4) -- (0,4);
\draw[dashed,color=lightgray] (-1,-1) rectangle (1,1);
\end{scope}
\begin{gnuplot}[terminal=tikz,terminaloptions={size 8,8}]
set contour
set cntrparam levels incremental -0.02,0.01,0.02
set view map
set view equal
unset surface
unset key
unset tics
unset border
set lmargin at screen 0
set rmargin at screen 1
set bmargin at screen 0
set tmargin at screen 1
set isosamples 1000,1000
set xrange [-3.5:3.5]
set yrange [-3.5:3.5]
set view 0,0
set cont base
splot x**3 + y**3 - 6*x*y
\end{gnuplot}
\end{tikzpicture}

\caption{A test of gnuplottex with epslatex}
\label{fig:gnuplot}
\end{figure}
\end{document}


Here's a solution to my problem from some solid fiddling last night: I could get pgfplots to work:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}

This is a test of pgfplots.

\begin{tikzpicture}
\draw[<->,color=lightgray] (-4,0) -- (4,0);
\draw[<->,color=lightgray] (0,-4) -- (0,4);
\draw[dashed] (-1,-1) rectangle (1,1);
\begin{axis}[x=1cm,y=1cm,z=1cm,
axis lines=none,
view={0}{90},
anchor=center]
samples=41,
domain=-3.5:3.5,
y domain = -3.5:3.5]
{x^3+y^3-6*x*y};
\end{axis}
\end{tikzpicture}

\end{document}


In this case anchoring the pgfplot axis at "center" seemed to do the trick.

Use the origin anchor to position the plot:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\draw[<->,color=lightgray] (-4,0) -- (4,0);
\draw[<->,color=lightgray] (0,-4) -- (0,4);
\draw[dashed] (-1,-1) rectangle (1,1);
\begin{axis}[
x=1cm,y=1cm,z=1cm,
axis lines=none,
view={0}{90},
anchor=origin% anchoring at origin
]
line width=2pt,
contour gnuplot={levels={0},
labels=false},
samples=41,
domain=-3.5:3.5,
y domain = -3.5:3.5
]{x^3+y^3-6*x*y};
\end{axis}
\end{tikzpicture}
\end{document}


But maybe it is better to draw the axis lines and the dashed rectangle inside the axis environment:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x=1cm,y=1cm,z=1cm,
axis z line=none,
axis x line=middle,
axis y line=middle,
axis line style={<->,lightgray},
xmin=-4,xmax=4,
ymin=-4,ymax=4,
view={0}{90},
]
\draw[dashed] (axis cs:-1,-1) rectangle (axis cs:1,1);