How to Repeat a PGFPlots Style Every n Plots
At a glance
- 1. Choose the group size
Decide how many consecutive plots should share a style, such as three plots per color.
- 2. Write the cycle list
Add one cycle-list entry for each plot; repeat the desired color in each group.
- 3. Plot in order
PGFPlots assigns entries successively to addplot commands and restarts the list when it runs out.
- 4. Check the legend
Each plot still gets its own legend entry unless you change the legend settings.
Title: How to Repeat a PGFPlots Style Every n Plots Category: latex
---- ARTICLE BODY ----
To make PGFPlots keep the same color for every group of plots, define a custom cycle list that repeats that color times. PGFPlots consumes one entry for each \addplot, in order. You can vary markers or line styles within each group while keeping its color constant.
Repeat a style in the cycle list
The cycle list key accepts a sequence of plot styles. Each style entry is used by the next \addplot command. For example, with three plots per color, include three entries using blue, then three using red:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
cycle list={
{blue,mark=*},
{blue,mark=square*},
{blue,mark=triangle*},
{red,mark=*},
{red,mark=square*},
{red,mark=triangle*},
},
legend entries={A1,A2,A3,B1,B2,B3},
]
\addplot coordinates {(0,1) (1,2) (2,1)};
\addplot coordinates {(0,2) (1,1) (2,3)};
\addplot coordinates {(0,3) (1,2) (2,2)};
\addplot coordinates {(0,1) (1,3) (2,4)};
\addplot coordinates {(0,2) (1,4) (2,3)};
\addplot coordinates {(0,4) (1,3) (2,5)};
\end{axis}
\end{tikzpicture}
\end{document}
The first three plots are blue and the next three are red. Markers change within each group, making individual plots easier to tell apart. The list contains six entries, so if you add more plots, PGFPlots starts again at the first entry after the sixth.
To keep every plot in a group visually identical, repeat the entire style rather than changing the marker. For example, use {blue,mark=*} three times in a row for a group of three.
General pattern for a group of n
For each color or other group-level style, provide consecutive entries. If the group should have different markers, use a different marker in each entry while keeping the color fixed. A group of four might look like this:
cycle list={
{blue,mark=*},
{blue,mark=square*},
{blue,mark=triangle*},
{blue,mark=diamond*},
{red,mark=*},
{red,mark=square*},
{red,mark=triangle*},
{red,mark=diamond*},
}
This advances to red after four plots. Add another four entries for the next group color. The cycle list is a literal sequence, so it does not automatically calculate a group size or generate new colors for an unlimited number of groups.
If you mean cycling through n styles repeatedly
Sometimes “cycle every n plots” means repeating the same block of styles over and over, rather than holding one color for plots. Define those styles once in the cycle list. PGFPlots already repeats the full list when it reaches the end. For example, a list of a solid blue line and a dashed red line alternates those two styles across plots and repeats after every two plots.
cycle list shift is different: it changes the starting position in a cycle list. It does not make PGFPlots advance only once per group of plots. For grouped behavior, write repeated entries explicitly.
Common mistakes
- Writing only one entry per group: PGFPlots advances once per
\addplot, so a single blue entry is used for one plot, not for a whole group. - Forgetting the list length: The list wraps at its end. If you have more plots than entries, styles start again from the first entry.
- Expecting automatic new colors: Repeating a finite custom list repeats its sequence. Add entries for each group you need, or use a different approach if you need a dynamically generated sequence.
- Assuming the legend groups plots: The cycle list controls plot styles, not legend layout. To display a legend, supply entries, for example with
legend entriesor\addlegendentry. Each eligible plot normally has its own entry; useforget plotto omit selected plots or adjust legend options for presentation.
For more combinations of color, marker, and line-style cycles, consult the PGFPlots manual’s cycle-list documentation.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.