如何自动调整 beamer 覆盖层中 pgfplots 图例的大小?

如何自动调整 beamer 覆盖层中 pgfplots 图例的大小?

我想使用幻灯片叠加功能逐步将图块添加到图表中。我知道如何做到。但是,图例并没有按照我想要的方式显示。pgfplots 将图例框的大小设置为所有幻灯片上的相同大小,等于其最大值。在我看来,它在第一张幻灯片上看起来很丑。

考虑以下 MWE

\documentclass{beamer}

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

\setmainfont{Libertinus Serif}
\setsansfont{Libertinus Sans}

\usepgfplotslibrary{colorbrewer}
\pgfplotsset{
    compat = 1.18
  , cycle list/Dark2-8
  }

\usetikzlibrary{overlay-beamer-styles}

\begin{document}
\begin{frame}{Test}
    \begin{figure}
        \centering
        \begin{tikzpicture}
            \pgfplotsset{
                width             =  \textwidth
              , height            =  0.7\textwidth
              , grid            = major
              , every axis plot/.append style = {
                    line width = 1.5pt
                  , no markers
                  , domain     = 0:1
                  , samples    = 500
                  }
            }
            \begin{axis}[
                xmin = 0
              , xmax = 1
              , legend style = {
                    anchor = north west
                  , at     = {(0.05, 0.95)}
                  , font   = \scriptsize
                  }
              , legend cell align = {left}
              ]
                \addplot+
                    coordinates {
                        (0, 0)
                        (0.45, 0)
                        (0.55, 1)
                        (1, 1)
                    };
                \addlegendentry{1}
                \addplot+[
                    samples    = 2
                  , visible on = <2->]{-0.135557 + 1.27111 * x};
                \addlegendentry[visible on = <2->]{a long legend line}
                \addplot+[visible on = <3->]
                    {0.0734743 - 2.49145 * x + 10.0335 * x^2 - 6.68901 * x^3};
                \addlegendentry[visible on = <3->]{3}
            \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{frame}
\end{document}

第一张幻灯片中的图表如下所示:

在此处输入图片描述

我想要这个:

在此处输入图片描述

然后是这个:

在此处输入图片描述

等等...

答案1

您可以使用\only<>{...}它来完全删除其他叠加层上的图例条目:

\documentclass{beamer}

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

%\setmainfont{Libertinus Serif}
%\setsansfont{Libertinus Sans}

\usepgfplotslibrary{colorbrewer}
\pgfplotsset{
    compat = 1.18
  , cycle list/Dark2-8
  }

\usetikzlibrary{overlay-beamer-styles}

\begin{document}
\begin{frame}{Test}
    \begin{figure}
        \centering
        \begin{tikzpicture}
            \pgfplotsset{
                width             =  \textwidth
              , height            =  0.7\textwidth
              , grid            = major
              , every axis plot/.append style = {
                    line width = 1.5pt
                  , no markers
                  , domain     = 0:1
                  , samples    = 500
                  }
            }
            \begin{axis}[
                xmin = 0
              , xmax = 1
              , legend style = {
                    anchor = north west
                  , at     = {(0.05, 0.95)}
                  , font   = \scriptsize
                  }
              , legend cell align = {left}
              ]
                \addplot+
                    coordinates {
                        (0, 0)
                        (0.45, 0)
                        (0.55, 1)
                        (1, 1)
                    };
                \addlegendentry{1}
                \addplot+[
                    samples    = 2
                  , visible on = <2->]{-0.135557 + 1.27111 * x};
                \only<2->{\addlegendentry{a long legend line}}
                \addplot+[visible on = <3->]
                    {0.0734743 - 2.49145 * x + 10.0335 * x^2 - 6.68901 * x^3};
                \only<3->{\addlegendentry{3}}
            \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{frame}
\end{document}

在此处输入图片描述

相关内容