% main=pgfplots.tex \section{Drawing axes and plots} \subsection{\TeX-dialects: \LaTeX, Con{\TeX}t, plain \TeX } \label{sec:tex:dialects}% \PGFPlots\ is compatible with \LaTeX, Con{\TeX}t and plain \TeX. The only difference is how to specify environments. This affects any \PGF/\Tikz-environments and all \PGFPlots-environments like axis, semilogxaxis, semilogyaxis and loglogaxis: \begin{description} \def\HEAD{% \small \lstset{boxpos=b,breaklines=false,aboveskip=3pt,belowskip=3pt}% %\hspace{-1cm}% \begin{tabular}{*{2}{p{4cm}}}% }% \item[\LaTeX:] \lstinline!\usepackage{pgfplots}! and {\HEAD \begin{lstlisting} \begin{tikzpicture} \begin{axis} ... \end{axis} \end{tikzpicture} \end{lstlisting} & \begin{lstlisting} \begin{tikzpicture} \begin{semilogxaxis} ... \end{semilogxaxis} \end{tikzpicture} \end{lstlisting} \\ \end{tabular}% } A small \LaTeX--example file can be found in \begin{lstlisting} doc/latex/pgfplots/pgfplotsexample.tex. \end{lstlisting} \item[Con{\TeX}t:] \lstinline!\usemodule[pgfplots]! and {\HEAD \begin{lstlisting} \starttikzpicture \startaxis ... \stopaxis \stoptikzpicture \end{lstlisting} & \begin{lstlisting} \starttikzpicture \startsemilogxaxis ... \stopsemilogxaxis \stoptikzpicture \end{lstlisting} \\ \end{tabular}% } A small Con{\TeX}t--example file can be found in \begin{lstlisting} doc/context/pgfplots/pgfplotsexample.tex. \end{lstlisting} \item[plain \TeX:] \lstinline!\input pgfplots.tex! and {\HEAD \begin{lstlisting} \tikzpicture \axis ... \endaxis \endtikzpicture \end{lstlisting} & \begin{lstlisting} \tikzpicture \semilogxaxis ... \endsemilogxaxis \endtikzpicture \end{lstlisting} \\ \end{tabular}% } A small plain--\TeX--example file can be found in \begin{lstlisting} doc/plain/pgfplots/pgfplotsexample.tex. \end{lstlisting} \end{description} You may need to set low--level drivers if you intend to use |dvipdfm|, see section~\ref{sec:drivers}. \subsection{A first plot} Plotting is done using \lstinline|\begin{axis} ... \addplot ...; \end{axis}|, where |\addplot| is an interface to the \Tikz\ |plot| commands. \begin{codeexample}[] \begin{tikzpicture} \begin{axis}[ xlabel=Cost, name=an axis, ylabel=Error] \addplot[color=red,mark=x] coordinates { (2,-2.8559703) (3,-3.5301677) (4,-4.3050655) (5,-5.1413136) (6,-6.0322865) (7,-6.9675052) (8,-7.9377747) }; \end{axis} \end{tikzpicture} \end{codeexample} \begin{codeexample}[] \begin{tikzpicture}% \begin{axis}[ xlabel=$x$, ylabel=$\sin(x)$, name=an axis, ] \addplot plot[id=sin] function{sin(x)}; \end{axis} \end{tikzpicture}% \end{codeexample} The \lstinline!plot coordinates! and \lstinline!plot function! commands are two of the several \Tikz\ ways to create plots, see section~\ref{sec:addplot} for more details\footnote{Please note that you need \lstinline{gnuplot} installed to use \lstinline{plot function}.}. The options `|xlabel|' and `|ylabel|' define axis descriptions. \subsection{Two plots in the same axis} Multiple \lstinline!\addplot!-commands can be placed into the same axis. % generated with this statement: %\addplot plot[id=filesuffix_noise,domain=-6:5,samples=10] function{(-x**5 - 242 + (-300 + 600*rand(0)))}; \begin{codeexample}[leave comments] \begin{tikzpicture} \begin{axis}[ height=9cm, width=9cm, grid=major, ] \addplot plot[id=filesuffix] function{(-x**5 - 242)}; \addlegendentry{model} \addplot coordinates { (-4.77778,2027.60977) (-3.55556,347.84069) (-2.33333,22.58953) (-1.11111,-493.50066) (0.11111,46.66082) (1.33333,-205.56286) (2.55556,-341.40638) (3.77778,-1169.24780) (5.00000,-3269.56775) }; \addlegendentry{estimate} \end{axis} \end{tikzpicture} \end{codeexample} A legend entry is generated if there are |\addlegendentry| commands (or one |\legend| command). \subsection{Logarithmic plots} Logarithmic plots show $\log x$ versus $\log y$ (or just one logarithmic axis). \PGFPlots\ always uses the natural logarithm, i.e. basis $e\approx2.718$. Now, the axis description also contains minor ticks and the labels are placed at $10^i$. \begin{codeexample}[] \begin{tikzpicture} \begin{loglogaxis}[xlabel=Cost,ylabel=Gain] \addplot[color=red,mark=x] coordinates { (10,100) (20,150) (40,225) (80,340) (160,510) (320,765) (640,1150) }; \end{loglogaxis} \end{tikzpicture} \end{codeexample} A common application is to visualise scientific data. This is often provided in the format $1.42\cdot10^4$, usually written as 1.42e+04. Suppose we have a numeric table named |pgfplots.testtable|, containing \begin{codeexample}[code only,tabsize=6] Level Cost Error 1 7 8.471e-02 2 31 3.044e-02 3 111 1.022e-02 4 351 3.303e-03 5 1023 1.038e-03 6 2815 3.196e-04 7 7423 9.657e-05 8 18943 2.873e-05 9 47103 8.437e-06 \end{codeexample} then we can plot |Cost| versus |Error| using \begin{codeexample}[] \begin{tikzpicture} \begin{loglogaxis}[ xlabel=Cost, ylabel=Error] \addplot[color=red,mark=x] coordinates { (5, 8.31160034e-02) (17, 2.54685628e-02) (49, 7.40715288e-03) (129, 2.10192154e-03) (321, 5.87352989e-04) (769, 1.62269942e-04) (1793, 4.44248889e-05) (4097, 1.20714122e-05) (9217, 3.26101452e-06) }; \addplot[color=blue,mark=*] table[x=Cost,y=Error] {pgfplots.testtable}; \legend{Case 1,Case 2} \end{loglogaxis} \end{tikzpicture} \end{codeexample} The first plot employs inline coordinates; the second one reads numerical data from file and plots column `|Cost|' versus `|Error|'. \noindent Besided the environment ``|loglogaxis|'' you can use \begin{itemize} \item \lstinline!\begin{axis}...\end{axis}! for normal plots, \item \lstinline!\begin{semilogxaxis}...\end{semilogxaxis}! for plots which have a normal~$y$ axis and a logarithmic~$x$ axis, \item \lstinline!\begin{semilogyaxis}...\end{semilogyaxis}! the same with $x$~and~$y$ switched, \item \lstinline!\begin{loglogaxis}...\end{loglogaxis}! for double--logarithmic plots. \end{itemize} You can also use \begin{lstlisting} \begin{axis}[xmode=normal,ymode=log] ... \end{axis} \end{lstlisting} which is the same as \lstinline!\begin{semilogyaxis}...\end{semilogyaxis}!. \begin{codeexample}[] \begin{tikzpicture} \begin{semilogyaxis}[ xlabel=Index,ylabel=Value] \addplot[color=blue,mark=*] coordinates { (1,8) (2,16) (3,32) (4,64) (5,128) (6,256) (7,512) }; \end{semilogyaxis}% \end{tikzpicture}% \end{codeexample} \subsection{Cycling line styles} You can skip the style arguments for \lstinline!\addplot[...]! to determine plot specifications from a predefined list: \label{page:plotcoords:src}% \begin{codeexample}[width=4cm] \begin{tikzpicture} \begin{loglogaxis}[ xlabel={Degrees of freedom}, ylabel={$L_2$ Error} ] \addplot coordinates { (5,8.312e-02) (17,2.547e-02) (49,7.407e-03) (129,2.102e-03) (321,5.874e-04) (769,1.623e-04) (1793,4.442e-05) (4097,1.207e-05) (9217,3.261e-06) }; \addplot coordinates{ (7,8.472e-02) (31,3.044e-02) (111,1.022e-02) (351,3.303e-03) (1023,1.039e-03) (2815,3.196e-04) (7423,9.658e-05) (18943,2.873e-05) (47103,8.437e-06) }; \addplot coordinates{ (9,7.881e-02) (49,3.243e-02) (209,1.232e-02) (769,4.454e-03) (2561,1.551e-03) (7937,5.236e-04) (23297,1.723e-04) (65537,5.545e-05) (178177,1.751e-05) }; \addplot coordinates{ (11,6.887e-02) (71,3.177e-02) (351,1.341e-02) (1471,5.334e-03) (5503,2.027e-03) (18943,7.415e-04) (61183,2.628e-04) (187903,9.063e-05) (553983,3.053e-05) }; \addplot coordinates{ (13,5.755e-02) (97,2.925e-02) (545,1.351e-02) (2561,5.842e-03) (10625,2.397e-03) (40193,9.414e-04) (141569,3.564e-04) (471041,1.308e-04) (1496065,4.670e-05) }; \legend{$d=2$,$d=3$,$d=4$,$d=5$,$d=6$} \end{loglogaxis} \end{tikzpicture} \end{codeexample} \noindent The cycle list can be modified, see the reference below. \subsection{Scaling plots} You can use any of the \Tikz\ options to modify the appearance. For example, the ``|scale|'' transformation takes the picture as such and scales it. {% \begin{codeexample}[] \begin{tikzpicture}[scale=0.5] \begin{loglogaxis}[ xlabel={Degrees of freedom}, ylabel={$L_2$ Error} ] \plotcoords \legend{$d=2$,$d=3$,$d=4$,$d=5$,$d=6$} \end{loglogaxis} \end{tikzpicture} \begin{tikzpicture}[scale=1.1] \begin{loglogaxis}[ xlabel={Degrees of freedom}, ylabel={$L_2$ Error} ] \plotcoords \legend{$d=2$,$d=3$,$d=4$,$d=5$,$d=6$} \end{loglogaxis} \end{tikzpicture} \end{codeexample} However, you can also scale plots by assigning a |width=5cm| and/or |height=3cm| argument. This only affects the distance of point coordinates, no font sizes or axis descriptions: \begin{codeexample}[] \begin{tikzpicture} \begin{loglogaxis}[ width=6cm, xlabel={Degrees of freedom}, ylabel={$L_2$ Error} ] \plotcoords \legend{$d=2$,$d=3$,$d=4$,$d=5$,$d=6$} \end{loglogaxis} \end{tikzpicture} \begin{tikzpicture} \begin{loglogaxis}[ width=8cm, xlabel={Degrees of freedom}, ylabel={$L_2$ Error} ] \plotcoords \legend{$d=2$,$d=3$,$d=4$,$d=5$,$d=6$} \end{loglogaxis} \end{tikzpicture} \end{codeexample}