2

I would like to have axis dimensions in real TeX units.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      xmin=0,
      xmax=1,
      ymin=0,
      ymax=1,
      xtick=\empty,
      ytick=\empty,
      width=4cm,
      height=2cm,
    ]
    \addplot[mark=none]{x};
  \end{axis}
\end{tikzpicture}
\rule{4cm}{2cm}
\end{document}

How to have the left rectangle of the tikz picture and the black rectangle of the TeX rule with exactly the same size?

enter image description here

The method should work whatever dimension is given (2cm and 4cm are only examples).

NB: The preamble was added after rmano's comment.

  • 1
    Hi, Welcome to TeX.SX. Please, post complete MWE next time. You can achieve what you need by adding scale only axis to the options of the axis environment. – Rmano Oct 13 at 9:29
  • 1
    As an addition to Rmano's answer: the reason the axis becomes smaller is that pgfplots subtracts a bit from the specified size to make room for ticklabels and axis labels (unless scale only axis is used). It doesn't currently do any calculation of the size of the labels, instead it just sets the size of the axis box to 45pt less than the specified size. Hence, in this case width=4cm+45pt, height=2cm+45pt, gives the same result as width=4cm, height=2cm, scale only axis. I'd rather use the scale only axis version though. – Torbjørn T. Oct 13 at 18:27
6

The trick is scale only axes, which applies the dimensions only to the axis (when you add ticks, titles and so on the bounding box of the graph will be bigger, of course).

\documentclass[]{article}
\usepackage{pgfplots}\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      xmin=0,
      xmax=1,
      ymin=0,
      ymax=1,
      xtick=\empty,
      ytick=\empty,
      width=4cm,
      height=2cm,
      scale only axis,
    ]
    \addplot[mark=none]{x};
  \end{axis}
\end{tikzpicture}
\rule{4cm}{2cm}
\end{document}

big boxes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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