Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am toying a bit with the pgfplot way of presenting data. It makes pretty nice plots, but unfortunately it is not very economical with my space. This is a problem when using a multi-column layout. Does anyone know how to decrease the extra white space margin around the plot, and possibly move the axis labels closer to the axes? As of now, I get much more "plot for my money" by doing it in matlab and just exporting to PDF.

If someone knows of a handy way of cropping PDF-images when inserting in LaTeX, that might also work, as I could export the pgf-plot to a PDF-file and then insert a cropped version of that.

share|improve this question
2  
Welcome to TeX.sx! If you add a minimal working example (MWE) that illustrates your spacing problem, people could maybe see where the problem is introduced. Normally there should be no large margin. –  Martin Scharrer Jun 28 '11 at 11:35
add comment

3 Answers

As Martin correctly said, usually there shouldn't be any unnecessary whitespace around the graph. Here's an example of a graph in a two column layout, with the background rectangle of the enclosing tikzpicture drawn using the backgrounds library:

\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage{float}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\begin{document}
\lipsum[1]
\begin{figure}[H]
\begin{tikzpicture}[show background rectangle,tight background]

\begin{axis}[
    width=\columnwidth,
    xlabel=Independent Variable,
    ylabel=Dependent Variable]
\addplot {1000*rand};
\end{axis}

\end{tikzpicture}
\caption{Standard options}
\end{figure}

\lipsum[2-6]
\end{document}

pgfplots in two column layout

To move the axis labels closer to the axes, you can use the options xlabel style={yshift=<length>}} and ylabel style={yshift=<length>}}. Note that you need to use yshift in both cases, even though the y axis label will be shifted horizontally. This is necessary because the shift will be applied before the label is rotated.

...
\begin{axis}[
    width=\columnwidth,
    xlabel style={yshift=0.75em},
    ylabel style={yshift=-0.5em},
    xlabel=Independent Variable,
    ylabel=Dependent Variable]
\addplot {1000*rand};
\end{axis}
...

pgfplots with shifted axis labels

In some cases, it might be desirable to change the apparent width of the plot, to allow the tick labels and axis label to exceed the image area. This can be achieved using trim axis left as an option to the tikzpicture that contains the axis environment. To still get the maximum plot width, you can use the option scale only axis, which means that size options only take into account the plot area, but ignore labels. This will only make sense in the left column.

...
\begin{tikzpicture}[show background rectangle,tight background,
    trim axis left]

\begin{axis}[
    scale only axis,
    width=\columnwidth,
    xlabel=Independent Variable,
    ylabel=Dependent Variable]
\addplot {1000*rand};
\end{axis}
...

pgfplots with trim axis left

share|improve this answer
add comment

AFAIK normally there should be no extra large margin around pgfplots (I don't use it myself this often). Anyway, you could trim any space around it manually by placing it between \begin{adjustbox}[trim=<left> <bottom> <right> <top>] ... \end{adjustbox} of the adjustbox package. This only trims the official size of the plot, if you also add the clip option any overhang is also visibly removed.

PDF images can be cropped using the pdfcrop tool which got created extra for use with LaTeX.

For the sake of completeness I also mentioned the standalone class which can be used to generate tightly cropped PDF (or DVI/PS) images from LaTeX code (using the great preview package). Turning complex pgfplots or tikzpictures into PDF can decrease the compile time a lot. See also the related external TikZ library. The next version of standalone will include some auto-compile and use-TeX-or-PDF feature.

share|improve this answer
    
@Jake: Thanks. Fixed. –  Martin Scharrer Jun 28 '11 at 13:08
add comment

You can crop your PDF images right in your LaTeX file as in the following example.

\begin{figure}[tb]
\centering
\includegraphics[width=80mm,trim=5 4 2 5 mm, clip=true]{image.pdf} %Left, bottom, right, top
\caption{Cropped image}  
\label{crop}
\end{figure}    
share|improve this answer
    
Welcome to TeX.sx! –  Peter Jansson May 3 '13 at 8:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.