0

When you specify width and height of a plot in pgfplots, the latter estimates the scaling so it can fit everything. The problem is that this estimate is too conservative. Consider the following MWE:

\documentclass[12pt, twoside, paper = B5]{scrbook}

\usepackage{fontspec} \usepackage{unicode-math} \usepackage{pgfplots}

\setmainfont{Libertinus Serif} \setmathfont{Libertinus Math}

\pgfplotsset{compat = 1.17} \usetikzlibrary{backgrounds}

\begin{document}

\noindent\begin{tikzpicture} \begin{scope}[on background layer] \fill[black!20] (0, 0) rectangle (57mm, 32mm); \end{scope} \begin{axis}[ anchor = outer south west , width = 57mm , height = 32mm , axis equal , axis lines = middle , axis line style = {-stealth, thick} , enlargelimits = false , tick style = {draw = none} , ymin = {0.0} , xtick = \empty , ytick = \empty , xlabel = {$x$} , ylabel = {$y$} , xlabel style = { at = {(current axis.right of origin)} , anchor = base west , font = \footnotesize } , ylabel style = { at = {(current axis.above origin)} , anchor = base west , font = \footnotesize } ] \addplot+[ no markers , thick , domain = -2.08869:8.37188 , samples = 201 ] ({x - 1.5 * sin(x r)}, {1.5 - 1.5 * cos(x r)}); \end{axis} \end{tikzpicture}

\end{document}

It produces the following output:

enter image description here

The gray rectangle designates the desired width and height. You can see that the actual plot size is far smaller than the specified dimensions. I know about scale only axis, but it will overshoot the size, I really need it to fit.

facetus
  • 837
  • Your goal is to fit the graph exactly inside the rectangle? For me scale only axis=true works fine. – Excelsior Apr 23 '21 at 07:29
  • Have you read the post? I specifically mentioned scale only axis. – facetus Apr 23 '21 at 07:38
  • I wrote the post. What do you mean with overshoot the size? – Excelsior Apr 23 '21 at 07:40
  • You wrote a comment to the post. The post is written by me. Here is a quote from the documentation: "If scale only axis is enabled, width and height apply only to the axis rectangle. Consequently, the resulting figure is larger that width and height (because of any axis descriptions)." – facetus Apr 23 '21 at 07:42
  • Yes, that is true, and which is what @Excelsior is saying. You draw a background rectangle that does not cover the tick labels. If you were to shift it such that it does, it will cover the plot including the ticks more or less precisely. –  Apr 23 '21 at 07:45
  • No, this is not what he is saying. If I shift the rectangle, the precision is still far from perfect. – facetus Apr 23 '21 at 07:49
  • 2
    The estimation is just a fixed value of 45pt, so it would be lucky if it fit perfectly ... Try this though: https://tex.stackexchange.com/a/561238/ – Torbjørn T. Apr 25 '21 at 09:06
  • @TorbjørnT. Thank you, got it. It's sad. TikZ pictures are slow per se, generating them twice is not encouraging. My own solution was kind of similar, though I generated just node {$x$} in a temporary box. Its width was all I needed to manually estimate the exact width. – facetus Apr 25 '21 at 16:33

2 Answers2

1

The estimation is not so bad --- it considers that you use labels for the axis for example. Moreover, it seems worse because you did not choose an anchor for the graph.

enter image description here

\documentclass[12pt, twoside, paper = B5]{scrbook}

\usepackage{fontspec} \usepackage{unicode-math} \usepackage{pgfplots}

\setmainfont{Libertinus Serif} \setmathfont{Libertinus Math}

\pgfplotsset{compat = 1.17} \usetikzlibrary{backgrounds}

\begin{document}

\noindent\begin{tikzpicture} \begin{scope}[on background layer] \fill[black!20] (0, 0) rectangle (57mm, 35mm); \end{scope} \begin{axis}[anchor=outer south west, width = 57mm , height = 35mm , axis equal , xlabel = $x$ , ylabel = $y$ ] \addplot+[ no markers , thick , domain = -2.08869:8.37188 , samples = 201 ] ({x - 1.5 * sin(x r)}, {1.5 - 1.5 * cos(x r)}); \end{axis} \end{tikzpicture}

\end{document}

Rmano
  • 40,848
  • 3
  • 64
  • 125
1

If you use plain TikZ, then you can control the sizes figure and explicit scaling.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[scale=.5]
\draw[red,thick,smooth] plot[domain=-.6*pi:4.6*pi,samples=500]
({\x - 1.5 * sin(\x r)}, {1.5 - 1.5 * cos(\x r)});
\def\a{1.5} \def\b{1.5}
\path 
(current bounding box.north east) +(\a,\b) coordinate (NE)
(current bounding box.south west) +(-\a,-\b) coordinate (SW)
;
\begin{scope}[on background layer]
\draw[fill=yellow!20] (NE) rectangle (SW);      
\draw[-stealth] (0,-1)--(0,4) node[left]{$y$};
\draw[-stealth] (-1,0)--(14,0) node[below=2pt]{$x$};
\draw[dashed] (14,3)--(0,3) node[left]{$3$};
\path 
(0,0) node[below left]{O}
(2*pi,0) node[below]{$2\pi$}
(4*pi,0) node[below]{$4\pi$}
;
\end{scope}
\end{tikzpicture}
\end{document}
Black Mild
  • 17,569