在 pgfplots 中合并循环列表

在 pgfplots 中合并循环列表

是否可以合并两个循环列表?也就是说,是否可以同时将几个循环列表应用于一个公共图,而不会让最后一个列表覆盖其他列表(即获得与命令类似的行为/.style append)?

例如,我有第一个标记列表和第二个颜色列表:

\documentclass{article}

\begin{document}

\pgfplotscreateplotcyclelist{list1}{%
{},
{mark=x},
{mark=+}}

\pgfplotscreateplotcyclelist{list2}{%
{color=red},
{color=blue},
{color=green}}



\begin{tikzpicture}
\begin{axis}
% Mising commands before plot 1
\addplot {x}; % should be red and have no marker
\addplot {x+1}; % should be blue and have `x' marker
\addplot {x+2}; % should be green and have `+' marker

% plot 2
\pgfplotsset{cycle list name=list1}
\addplot {2*x}; % is red and has default marker
\addplot {2*x+1}; % is blue and has default marker
\addplot {2*x+2}; % is green and has default marker


% plot 3
\pgfplotsset{cycle list name=list2}
\addplot {-x}; % is default colour and has no marker
\addplot {-x+1}; % is default colour and has `x' marker
\addplot {-x+2}; % is default colour and has `+' marker
\end{axis}

\end{tikzpicture}
\end{document}

有时我想只使用标记,有时只使用颜色,有时同时使用两者。这与cycle multi list在第二个列表每次重置时先增加最后一个列表,然后增加第一个列表不同。当然,我可以创建第三个列表来手动组合list1list2但我想知道是否可以更有效地实现这一点?

当在长文档中使用多个功能(颜色、标记、线条样式等)列表并以各种组合使用它们时,此功能会派上用场。在这种情况下,写下所有组合很麻烦,而且,比如说,更改一种颜色意味着要跟踪它在每种组合中的完成情况。

答案1

是的:只需更改(一组)图之前的循环列表名称:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer}

\begin{document}

\pgfplotscreateplotcyclelist{list1}{%
{},
{mark=x},
{mark=+}}

\pgfplotscreateplotcyclelist{list2}{%
{color=red},
{color=blue},
{color=green}}

\begin{tikzpicture}
    \begin{axis}
    \pgfplotsset{cycle list name=list1}
    \addplot {x};
    \addplot {x+1};

    \pgfplotsset{cycle list name=list2}
    \addplot {5-x};
    \addplot {6-x};
    \end{axis}
\end{tikzpicture}
\end{document}

两者都会同时递增。实际上,它们都没有“递增”,但当前绘图索引被用作当前列表中的查找索引。并且会\pgfplotsset更改“当前列表”。

答案2

我找到了一种部分解决我的问题的权宜之计。诀窍是反过来做:编写组合列表并禁用不需要的功能。它对标记很有效,但对颜色并不完全有效(黑色而不是默认颜色)。它也不太有用,因为当有多个标记列表或颜色列表时,必须编写多个组合列表。

\documentclass{article}
\tikzset{no colour/.style={color=black}}
\tikzset{no marker/.style={maker={}}}
\begin{document}

\pgfplotscreateplotcyclelist{list3}{%
{color=red},
{color=blue,mark=x},
{color=green,mark=+}}



\begin{tikzpicture}
\begin{axis}
% plot 1
\addplot {x}; % is red and has no marker
\addplot  {x+1}; % is blue and has `x' marker
\addplot  {x+2}; % is green and has `+' marker

% plot 2
\addplot+[no marker] {2*x}; % is red and has default marker
\addplot+[no marker] {2*x+1}; % is blue and has default marker
\addplot+[no marker] {2*x+2}; % is green and has no marker


% plot 3
\addplot+[no colour] {-x}; % is black and has no marker
\addplot+[no colour] {-x+1}; % is black and has `x' marker
\addplot+[no colour] {-x+2}; % is black and has `+' marker
\end{axis}
\end{tikzpicture}

\end{document}

相关内容