I need to change the size of a PGFPlots tikzpicture in my LaTeX document. There are two different things, scaling the graphic to the full or half width for example and changing the aspect ratio of a graphic.
We will start with a minimal example for a PGFPlots tikzpicture in a tex document that can be compiled to a PDF document with the following command for example.
pdflatex example.tex
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {
(0, 8)
(4, 2)
(8, 6)
};
\end{axis}
\end{tikzpicture}
\end{document}
The resulting arrangement of the graphic should look something like this on the PDF page.
The first approach is resizebox
that I have found as answer to the question
Correctly scaling a tikzpicture.
We can scale a graphic with resizebox
to the full textwidth
the following way. The aspect
ration of the graphic will persist, cause we set the height to !
.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\resizebox {\textwidth} {!} {
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {
(0, 8)
(4, 2)
(8, 6)
};
\end{axis}
\end{tikzpicture}
}
\end{document}
The scaled graphic with resizebox should look something like this on the PDF page.
0.5\textwidth
to scale the graphic to the half of the
textwidth for example.
If we use \height
instead of !
for the height argument the aspect ratio is changing and
we scale only the width of the graphic and the height will persist.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\resizebox {\textwidth} {\height} {
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {
(0, 8)
(4, 2)
(8, 6)
};
\end{axis}
\end{tikzpicture}
}
\end{document}
An other solution for my example are the parameters width
and height
of the
axis
that I have found as answer in the question
how to set plot box aspect ratio.
We can scale the example graphic with persistant aspect ratio to the full textwidth via the parameter
width
for axis
.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=\textwidth]
\addplot coordinates {
(0, 8)
(4, 2)
(8, 6)
};
\end{axis}
\end{tikzpicture}
\end{document}
The difference to the resizebox is that the graphic got more details instead of just scaling an image like resource.
By setting the height to axisdefaultheight
we can scale only the width of the graphic and the aspect
ratio will change, but the height will persist.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
height=\axisdefaultheight]
\addplot coordinates {
(0, 8)
(4, 2)
(8, 6)
};
\end{axis}
\end{tikzpicture}
\end{document}
Again is the difference to the resizebox that the graphic got more detail instead of just scaling.