# Scaling and trimming white space from a pgfplots picture

In answering How do I create a combined figure with two imported curves and draw around it with Tikz?, I was able to make my own graph fill the space between two horizontal lines as seen here. However, when I tried to import a dat file, I ran into difficulties.

My first problem was scaling. I thought I could use the key height to fix the height of the image. But it's not behaving as I thought it would:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\def\myimage#1{%%
\begin{tikzpicture}%%[x=#1,y=#1]
\begin{axis}[
height=#1,
hide axis,
no markers,
]
\addplot + table[x index=0, y index=1]{vp1.dat};
\end{axis}
\end{tikzpicture}}

\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\draw (0,0) -- ++(6,0)
(0,4) -- ++(6,0);
\node[inner sep=0pt,draw,red] at (3,2) {\myimage{4cm}};
\end{tikzpicture}

\end{document}


I wanted it so that the red box's top and bottom should be flush with the black horizontal lines.

My second problem was getting rid of all the white space. I would like the bounding box for the graph to fit as tightly as possible when I've hidden the axes. In other words, the red border should be snug up against the blue of the graph.

Here's my vp1.dat file:

1 3
2 4
3 -5
4 6


Here's my vp2.dat file:

1 3
2 4
3 1
4 3


## Update

I have partial solutions to my questions but no combined solution.

Solution to first problem

Regarding the first problem, I can get the pgfplot to fill the desired effect by using a combination of

scale only axis
height=#2


MWE:

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\def\myimage#1#2{%%
\begin{tikzpicture}
\begin{axis}[
scale only axis,
height=#2,
hide axis,
no markers,
]
\addplot + table[x index=0, y index=1]{#1};
\end{axis}
\end{tikzpicture}}

\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\draw (0,0) -- ++(6,0)
(0,4) -- ++(6,0);
\node[anchor=south] at (3,4) {\texttt{height=4cm} passed to \texttt{pgfplots}};
\node[inner sep=0pt,draw,red] at (3,2) {\myimage{vp1.dat}{4cm}};
\draw[<->] (6.25,0) -- ++(0,4) node [midway,right]{4cm};
\end{tikzpicture}

\end{document}


which results in:

Solution to second problem

Regarding the second problem, I can get the pgfplot to trim the extra white space that I do not want by using both

clip bounding box=upper bound
hide axis


MWE:

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\def\myimage#1#2{%%
\begin{tikzpicture}
\begin{axis}[
clip bounding box=upper bound,
hide axis,
no markers,
]
\addplot + table[x index=0, y index=1]{#1};
\end{axis}
\end{tikzpicture}}

\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\node[anchor=south] at (3,4.35) {graph fits snuggly in its bounding box};
\node[inner sep=0pt,draw,red] at (3,2) {\myimage{vp1.dat}{4cm}};
\end{tikzpicture}

\end{document}


which results in:

No solution for combined approach

But when I try to combine both methods, the height is not obeyed properly and I can't figure out how it's being calculated.

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\def\myimage#1#2{%%
\begin{tikzpicture}
\begin{axis}[
scale only axis,
clip bounding box=upper bound,
height=#2,
hide axis,
no markers,
]
\addplot + table[x index=0, y index=1]{#1};
\end{axis}
\end{tikzpicture}}

\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\draw (0,0) -- ++(6,0)
(0,4) -- ++(6,0);
\node[anchor=south] at (3,4) {combining \texttt{clipping} and \texttt{height}};
\node[inner sep=0pt,draw,red] at (3,2) {\myimage{vp1.dat}{4cm}};
\draw[<->] (6.25,0) -- ++(0,4) node [midway,right]{4cm};
\end{tikzpicture}
\end{document}


which results in:

• Related Questions: Perhaps Specifying the width and height of a tikzpicture might be of some help for the first problem. And for the second question: Is there a way to control whitespace around a TikZ picture?. – Peter Grill Apr 11 '14 at 1:56
• @PeterGrill I'm not sure how the first link helps. I'm already trying to use height, but it's not being obeyed. Regarding the second link, the white space I'm objecting to is being generated by pgfplots. With nodes in tikz you can specify inner sep=0pt to eliminate extra white space about the node. But, I can't do that here. For some reason pgfplots is maintaining the space dedicated to the axes even though I don't want to see them. How can I eliminate that dedicated space? – A.Ellett Apr 11 '14 at 2:04
• @PeterGrill I know I could use clib, but it seems that that's more of a hack. It seems to me that there should be some setting within pgfplots to achieve this. – A.Ellett Apr 11 '14 at 2:05
• I have found one answer: the extra white space can be eliminated by setting clip bounding box=upper bound when used in conjunction with hide axis. – A.Ellett Apr 11 '14 at 2:18
• I answered those questions a while ago so don't know. But the first one basically was that it does not make sense to specify a height for a tikzpicture as the drawing is done as per the x, y unit vectors of the tikzpicture. – Peter Grill Apr 11 '14 at 2:26

You want enlargelimits=false combined with scale only axis and height. pgfplots adds some whitespace around the plot by default, enlargelimits=false removes that.

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\def\myimage#1#2{%%
\begin{tikzpicture}
\begin{axis}[
scale only axis,
height=#2,
hide axis,
no markers,
enlargelimits=false
]
\addplot + table[x index=0, y index=1]{#1};
\end{axis}
\end{tikzpicture}}

\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\draw (0,0) -- ++(6,0)
(0,4) -- ++(6,0);
\node[anchor=south] at (3,4) {\texttt{enlargelimits}, \texttt{scale only axis} and \texttt{height}};
\node[inner sep=0pt,draw,red] at (3,2) {\myimage{vp1.dat}{4cm}};
\draw[<->] (6.25,0) -- ++(0,4) node [midway,right]{4cm};
\end{tikzpicture}
\end{document}

• Awesome. This is the solution I was looking for. – A.Ellett Apr 11 '14 at 12:02
• Good answer. Just a note for the knowledge base: \pgfplotsset{compat=1.8} or newer will always activate clip bounding box=upper bound. – Christian Feuersänger Apr 14 '14 at 18:35

I hope someone can come up with an answer which sticks entirely with tikz or pgfplots directives.

Here's a solution that lets me both clip the extra white space and set the box to the desired height.

My approach is to use pgfplots ability to clip the extra white space via

hide axis
clip bounding box=upper bound


Then I save the graph to a box (or, I could put it in its own standalone file). Then I scale according to the size of the saved box.

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}

\def\myimageSnug#1{%%
\begin{tikzpicture}
\begin{axis}[
clip bounding box=upper bound,
hide axis,
hide axis,
no markers,
]
\addplot + table[x index=0, y index=1]{#1};
\end{axis}
\end{tikzpicture}}

\pagestyle{empty}
\begin{document}

\newsavebox{\myimagebox}
\savebox{\myimagebox}{\myimageSnug{vp1.dat}}
\begin{tikzpicture}
\draw (0,0) -- ++(6,0)
(0,4) -- ++(6,0);
\node[anchor=south] at (3,4.35) {\footnotesize graph fits snuggly in its bounding box \textbf{and} it's the correct height};

\node[inner sep=0pt,draw,red,scale=4cm/\ht\myimagebox] at (3,2) {\usebox{\myimagebox}};
\end{tikzpicture}%%

\end{document}


resulting in

Alternatively, I could use a standalone file named graph_01:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
clip bounding box=upper bound,
hide axis,
hide axis,
no markers,
]
\addplot + table[x index=0, y index=1]{vp1.dat};
\end{axis}
\end{tikzpicture}

\end{document}


And then import it:

\documentclass{article}
\usepackage{graphicx}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\draw (0,0) -- ++(6,0)
(0,4) -- ++(6,0);
\node[anchor=south] at (3,4.35) {\footnotesize graph fits snuggly in its bounding box \textbf{and} it's the correct height};

\node[inner sep=0pt,draw,red] at (3,2) {\includegraphics[height=4cm]{graph_01}};
\end{tikzpicture}%%

\end{document}