如何改进“pgfplots”中的绘图大小估计?

如何改进“pgfplots”中的绘图大小估计?

当您在 中指定widthheight的图时pgfplots,后者会估计缩放比例,以便可以拟合所有内容。问题是这个估计太保守了。考虑以下 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}

它产生以下输出:

在此处输入图片描述

灰色矩形指定所需的宽度和高度。您可以看到实际绘图大小远小于指定的尺寸。我知道scale only axis,但它会超出尺寸,我真的需要它适合。

答案1

估计结果还不错——例如,它考虑到你为轴使用了标签。此外,由于你没有为图表选择锚点,所以结果似乎更糟。

在此处输入图片描述

\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}

答案2

如果您使用普通的 TikZ,那么您可以控制尺寸图形和明确的缩放比例。

在此处输入图片描述

\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}

相关内容