使用 beamer 命令切换 pgfplots 选项

使用 beamer 命令切换 pgfplots 选项

我正在尝试使用类似于此处提出的解决方案在图形中切换 x 轴的标签;MWE 是这样的:

\documentclass[12pt,t, fleqn, 
    %,handout %% "handout" for one page per slide 
]{beamer} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=1.9, width=8cm} 
\tikzset{
    onslide/.code args={<#1>#2}{% https://tex.stackexchange.com/a/6155/16595
        \only<#1>{\pgfkeysalso{#2}}
    },
    alt/.code args={<#1>#2#3}{%
        \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
    },
}

\begin{document}
\begin{frame}
\frametitle{Title}

\begin{tikzpicture}[
    ]
        \begin{axis}[
            %font=\scriptsize,
            xmin=-0.5, xmax=10, domain=0:10,
            ymin=-1, ymax=9,
            xlabel = {$t$},
            xtick = {0,1,...,9},
            onslide = {<2->{xticklabels={$0$,$T$,,,,,,,$nT$,},}},%
            % xticklabels={$0$,$T$,,,,,,,$nT$,}, % uncomment this and it works
            ]
            \addplot [alt={<1>{color=blue}{color=gray, thick}}, 
                %visible on=<1>,
                ] {0.8*x};
            \only<2->{\addplot [red, samples=200,] {0.6*x};}
        \end{axis}
\end{tikzpicture}

\end{frame}
\end{document}

但它不起作用---似乎当我输入xticklabels条件时onslide,密钥不再被识别:

! Package pgfkeys Error: I do not know the key '/tikz/xticklabels', to
which you passed '$0$,$T$,,,,,,,$nT$,', and I am going to ignore it.
Perhaps you misspelled it.

请注意,如果我使用第二行(注释的,无条件的)xticklabels 认可。

我在这里遗漏了什么?

请注意,alt颜色工作正常 --- 结果是

结果显示问题

PS:我找到了一种解决方法;我可以使用

\only<2->{\pgfplotsset{xticklabels={$0$,$T$,,,,,,,$nT$,}}}

在框架中外部tikzpicture似乎可以工作,但尽管如此,它并不优雅... ;-)

解决方法后得到正确结果

答案1

这是来自@percusse 的代码的一个工作示例。

幻灯片

\documentclass[12pt,t, fleqn, 
    %,handout %% "handout" for one page per slide 
]{beamer} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=1.9, width=8cm} 
%% Notice that if you change this to \pgfplotsset{...} then 
%% "onslide" will work directly in "axis" environment, although
%% not on plain "tikzpictures" ones! 
\tikzset{
    onslide/.code args={<#1>#2}{% http://tex.stackexchange.com/a/6155/16595
        \only<#1>{\pgfkeysalso{#2}}
    },
    alt/.code args={<#1>#2#3}{%
        \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
    },
}

\begin{document}
\begin{frame}
\frametitle{Title}

\begin{tikzpicture}
        \begin{axis}[
            xmin=-0.5, xmax=10, domain=0:10,
            ymin=-1, ymax=9,
            xlabel = {$t$},
            xtick = {0,1,...,9},
            onslide = <2->{/pgfplots/xticklabels={$0$,$T$,,,,,,,$nT$,}},%
            ]
            \addplot [alt={<1>{color=blue}{color=gray, thick}}, 
                %visible on=<1>,
                ] {0.8*x};
            \only<2->{\addplot [red, samples=200,] {0.6*x};}
        \end{axis}
\end{tikzpicture}

\end{frame}
\end{document}

相关内容