1

pgfplots offers the convenient feature of cycle list which helps a lot to keep consistent line styles throughout several plots. However, I have typically different line styles (assume some kind of repeating underlying data or equations) which appear rather randomly. Accessing the suitable entry of the cycle list with cycle list shift is rather cumbersome and requires manual changes if I add or remove curves in the plot.

I have made a simple example. The first plot contains polynomials from linear to fifth order, the second one only the odd ones (i.e., an arbitrary subset of the first curves plot).

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
% First axis, three types of plots
\begin{tikzpicture}
\begin{axis}[
domain=-1:1,legend entries={1,...,5},legend pos=south east,
cycle list name=exotic,
]
\addplot {x};
\addplot {x^2};
\addplot {x^3};
\addplot {x^4};
\addplot {x^5};
\end{axis}
\end{tikzpicture}

% Second axis, only some type of plots (here: only odd polynomials)
\begin{tikzpicture}
\begin{axis}[
domain=-1:1,legend entries={1,3,5},legend pos=south east,
cycle list name=exotic,
]
\addplot {x};
\pgfplotsset{cycle list shift=1}
\addplot {x^3};
\pgfplotsset{cycle list shift=2}
\addplot {x^5};
\end{axis}
\end{tikzpicture}
\end{document}

Exemplary output, the choice of the cycle list is arbitrary:

enter image description here

The consistent line styles are here achieved by manipulating the cycle list shift number. I would prefer a solution with an option in the style of \addplot[cycle list entry=1] or even with a custom label \addplot[cycle list entry=linear]. My question is, can this be done in pgfplots?

The only way I can think of to achieve the desired behavior is a manual one, i.e., defining a macro containing the desired plot options as

\pgfplotsset{%
  apply style/.code={%
    \tikzset{#1}%
  }
}
\def\linear{blue, dashed}

Then, the plot can be added as

\addplot[apply style/.expand once=\linear] {x};

I could not find this kind of feature in the pgfplots manual, but I assume it could be useful to many users.

crateane
  • 2,217
  • 1
    related question/answer which sets the cycle list counter to a fixed value: https://tex.stackexchange.com/questions/50874/using-counter-values-as-part-of-style-name-for-plots – crateane Feb 28 '20 at 11:40

1 Answers1

0

The best way I found so far is not related to cycle lists, but works with defining custom tikz styles. It still could be enhanced if the style is defined by accessing the nth entry of a cycle list, but I do not know how to achieve this.

Defining custom styles:

\tikzset{
  general/.style  = {thick,mark options={solid}},
  linear/.style   = {general,green,mark=+},
  quadratic/.style= {general,red,mark=x,},
  cubic/.style    = {general,blue,dashed,mark=square},
%...
}

Using them:

\addplot[style=linear] {x};
\addplot[style=cubic] {x^3};
\addplot[style=quintic] {x^5};

instead of the relevant part of the question, i.e., instead of

\addplot {x};
\pgfplotsset{cycle list shift=1}
\addplot {x^3};
\pgfplotsset{cycle list shift=2}
\addplot {x^5};
crateane
  • 2,217