[Tex/LaTex] Caption tikzpicture

captionsfloatstikz-pgf

I am getting an out of float error, here is the code:

\documentclass{article} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}    

\begin{center}
    \begin{tikzpicture}
            \begin{axis}[%
                ,xlabel=$N$
                ,ylabel=speedup
                ,axis x line=bottom
                ,axis y line=left
                ]
                \addplot[very thick] coordinates {(755,1.4) (1978, 1.6) (6273, 1.8) (12222, 2.1)};
            \end{axis}
    \end{tikzpicture}
\end{center}
% Where to put the caption?
%\caption{\label{plot:speedup}Speedup of distributed execution, versus serial. N is the leading dimension of the matrix.}

\end{document}

Best Answer

You get an out of float error because you used a \caption out side the float environment known as figure. A \caption can only be inserted between \begin{<float environment>} ... \end{<float environment>}.

Put the \caption after the end of tikzpicture, directly after the \end{tikzpicture} line. Also, use \centering instead of \begin{center} ... \begin{center} inside float environments to get consistent spacing around floats. Finally, for readability purposes, drop the \label on a separate line after \caption.

enter image description here

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

\begin{figure}    
\centering
\begin{tikzpicture}
       \begin{axis}[%
            ,xlabel=$N$
            ,ylabel=speedup
            ,axis x line=bottom
            ,axis y line=left
            ]
            \addplot[very thick] coordinates {(755,1.4) (1978, 1.6) (6273, 1.8) (12222, 2.1)};
        \end{axis}
\end{tikzpicture}
\caption{Speedup of distributed execution, versus serial. N is the leading dimension of the matrix.}
\label{plot:speedup}
\end{figure}

As we see in figure \ref{plot:speedup} ...

\end{document}

Another option if you don't like the figure environment is to use \captionof{<float>} from the caption package. You will get the same result.

\documentclass{article} 
\usepackage{pgfplots,caption}
\pgfplotsset{compat=1.9}
\begin{document}

\begin{center}
\begin{tikzpicture}
            \begin{axis}[%
                ,xlabel=$N$
                ,ylabel=speedup
                ,axis x line=bottom
                ,axis y line=left
                ]
                \addplot[very thick] coordinates {(755,1.4) (1978, 1.6) (6273, 1.8) (12222, 2.1)};
            \end{axis}
\end{tikzpicture}
\captionof{figure}{Speedup of distributed execution, versus serial. N is the leading dimension of the matrix.}
\label{plot:speedup}
\end{center}

As we see in figure \ref{plot:speedup} ...

\end{document}