设置循环列表位置的命令?

设置循环列表位置的命令?

是否有命令可以(重新)设置循环列表的起点?

我现在正在使用cycle list shift命令,但我最终做了一些耗时的计算,并且我的代码通常不太健壮,因为它使用了循环列表中的元素数量,而我可能希望随后更改这些元素数量。

有没有类似的东西set cyle list at n


编辑:为了澄清杰克提出的观点,这里有我所理解的两种可能模式的例子。

模式 1:

 \addplot    <- uses style 0
 \addplot    <- uses style 1
 \addplot    <- uses style 2

\pgfplotsset{cycle list set=5}
 \addplot    <- uses style 5
 \addplot    <- uses style 6
 \addplot    <- uses style 7

模式 2:

 \addplot    <- uses style 0
 \addplot    <- uses style 1
 \addplot    <- uses style 2

\pgfplotsset{cycle list set just once=5}
 \addplot    <- uses style 5
 \addplot    <- uses style 4
 \addplot    <- uses style 5

当我问这个问题时我想的是mode 1

模式 2 也可能很有趣,但也许作为一种风格:

 \addplot[set cycle list=5]    <- uses style 5

还可能有Mode 3

 \addplot    <- uses style 0
 \addplot    <- uses style 1
 \addplot    <- uses style 2

\pgfplotsset{cycle list set=5}
 \addplot    <- uses style 5
 \addplot    <- uses style 5
 \addplot    <- uses style 6

但这或多或少相当于mode 2(只需移动一步就可以轻松处理cycle list shift)。

答案1

可能有更优雅的方法来做到这一点,但有一件事是有效的,那就是修补 PGFPlots 的一个内部函数,以提供一个新密钥cycle list set

\begin{axis}[domain=0:10]
\addplot {x};
\addplot {2*x};
\addplot {3*x};
\pgfplotsset{cycle list set=0}
\addplot {4*x};
\addplot {5*x};
\addplot {6*x};
\end{axis}

\documentclass{article}

\usepackage{pgfplots}

\makeatletter
\def\pgfplots@getautoplotspec into#1{%
    \begingroup
    \let#1=\pgfutil@empty
    \pgfkeysgetvalue{/pgfplots/cycle multi list/@dim}\pgfplots@cycle@dim
    %
    \let\pgfplots@listindex=\pgfplots@numplots
    %%% Start new code
    \pgfkeysgetvalue{/pgfplots/cycle list set}\pgfplots@listindex@set
    \ifx\pgfplots@listindex@set\pgfutil@empty
    \else 
        \c@pgf@counta=\pgfplots@listindex
        \c@pgf@countb=\pgfplots@listindex@set
        \advance\c@pgf@countb by -\c@pgf@counta
        \globaldefs=1\relax
        \edef\setshift{%
            \noexpand\pgfkeys{
                /pgfplots/cycle list shift=\the\c@pgf@countb,
                /pgfplots/cycle list set=
            }
        }%
        \setshift%
    \fi
    %%% End new code    
    \pgfkeysgetvalue{/pgfplots/cycle list shift}\pgfplots@listindex@shift
    \ifx\pgfplots@listindex@shift\pgfutil@empty
    \else
        \c@pgf@counta=\pgfplots@listindex\relax
        \advance\c@pgf@counta by\pgfplots@listindex@shift\relax
        \ifnum\c@pgf@counta<0
            \c@pgf@counta=-\c@pgf@counta
        \fi
        \edef\pgfplots@listindex{\the\c@pgf@counta}%
    \fi
    \ifnum\pgfplots@cycle@dim>0
        % use the 'cycle multi list' feature.
        %
        % it employs a scalar -> multiindex map like
        % void fromScalar( size_t d, size_t scalar, size_t* Iout, const size_t* N )
        % {
        %   size_t ret=scalar;
        %   for( int i = d-1; i>=0; --i ) {
        %       Iout[i] = ret % N[i];
        %       ret /= N[i];
        %   }
        % }
        % to get the different indices into the cycle lists.
        %-------------------------------------------------- 
        \c@pgf@counta=\pgfplots@cycle@dim\relax
        \c@pgf@countb=\pgfplots@listindex\relax
        \advance\c@pgf@counta by-1
        \pgfplotsloop{%
            \ifnum\c@pgf@counta<0
                \pgfplotsloopcontinuefalse
            \else
                \pgfplotsloopcontinuetrue
            \fi
        }{%
            \pgfkeysgetvalue{/pgfplots/cycle multi list/@N\the\c@pgf@counta}\pgfplots@cycle@N
            % compute list index:
            \pgfplotsmathmodint{\c@pgf@countb}{\pgfplots@cycle@N}%
            \divide\c@pgf@countb by \pgfplots@cycle@N\relax
            %
            \expandafter\pgfplots@getautoplotspec@
                \csname pgfp@cyclist@/pgfplots/cycle multi list/@list\the\c@pgf@counta @\endcsname
                {\pgfplots@cycle@N}%
                {\pgfmathresult}%
            \t@pgfplots@toka=\expandafter{#1,}%
            \t@pgfplots@tokb=\expandafter{\pgfplotsretval}%
            \edef#1{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
            \advance\c@pgf@counta by-1
        }%
    \else
        % normal cycle list:
        \pgfplotslistsize\autoplotspeclist\to\c@pgf@countd
        \pgfplots@getautoplotspec@{\autoplotspeclist}{\c@pgf@countd}{\pgfplots@listindex}%
        \let#1=\pgfplotsretval
    \fi
    \pgfmath@smuggleone#1%
    \endgroup
}

\pgfplotsset{
    cycle list set/.initial=
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:10]
\addplot {x};
\addplot {2*x};
\addplot {3*x};
\pgfplotsset{cycle list set=0}
\addplot {4*x};
\addplot {5*x};
\addplot {6*x};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容