2

Is it possible to reverse the order of a cycle list in pgfplots, and if so, how can I do it?

I am using the pgfplots library colorbrewer to color a graph with four plots on it. I selected the palette OrRd because it is has a four-color palette that the colorbrewer website indicates is colorblind and photocopy safe. The palette includes a very light color that is hard to see against the white background of the plot. I would like to use the four darkest colors from the five or six color OrRd palettes, but if I set one of those palettes as the active cycle list, they use the lighter colors first. I would like to issue a command to reverse the order of the cycle list so that the first plot uses the darkest colors and then later plots use lighter colors.

Here is a minimal working example (adapted from Stefan Pinnow's answer below):

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{colorbrewer}
    \pgfplotsset{
    compat=1.16, cycle list/OrRd-6,}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        samples=2,
    ]
        \foreach \i in {1,...,4} {
            \addplot+ [very thick] {\i};
        }
    \end{axis}
\end{tikzpicture}
\end{document}

Here is the output of the above code: pgfplots follows the cycle list order and plots lines with lighter colors first and then darker colors. In this case, the lightest four colors of OrRd-6 are used.

Results of above code

Following is the output I desire: pgfplots follows the cycle list in reverse order. It plots using the darkest colors first, and then uses lighter colors.

Desired output

  • 3
    I guess you might want to provide us with an MWE. Most likely you just need to flip as sign somewhere, but without MWE this is hard to tell/describe. – user121799 Apr 19 '18 at 2:44
  • 1
    Did my answer answer your question? If yes, please consider upvoting (by clicking on the arrows next to the score) it and/or marking it as the accepted answer (by clicking on the checkmark ✓). If not, please edit your question again and state what you are missing. – Stefan Pinnow Apr 24 '18 at 4:32
  • I have updated the question with an MWE and an example of the desired output. I have accepted Stefan Pinnow's answer. – RobotRaven May 7 '18 at 17:53
5

Thanks to the newly introduced commands in PGFPlots v1.14 reversing the colors of a colormap is quite easy. The only thing remaining is then mapping the colors of the reversed colormap to a cycle list.

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{colorbrewer}
    \pgfplotsset{
        % load the `cycle list' or `colormap' that contains the colors you want to use
        cycle list/OrRd-6,
        % define own `cycle list' by first creating a custom `colormap' where
        % the colors are reversed ...
        colormap={OrRd-6-reversed}{
            indices of colormap=(
                \pgfplotscolormaplastindexof{OrRd-6},...,0 of OrRd-6
            )
        },
        % ... which then is mapped to a `cycle list'
        cycle list/.define={OrRd-6-reversed}{[of colormap=OrRd-6-reversed]},
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % use the custom `cycle list'
        cycle list name=OrRd-6-reversed,
        samples=2,
    ]
        \foreach \i in {1,...,4} {
            \addplot+ [very thick] {\i};
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

5

Stefan Pinnow's solution achieves the desired result, and it provides a way to truly reverse a cycle list, so it answers the question. However, I prefer to minimize the number of commands used to achieve a desired result, so I am posting my solution, which some users may prefer because it only requires a single additional command.

I can get the desired output by setting the cycle list shift key to -(length of cycle list - 1). This approach is simple to implement, but the value of cycle list shift must be customized for each cycle list.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{colorbrewer}
        \pgfplotsset{
    compat=1.16, cycle list/OrRd-6,}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        samples=2,
        cycle list shift=-5
    ]
        \foreach \i in {1,...,4} {
            \addplot+ [very thick] {\i};
        }
    \end{axis}
\end{tikzpicture}
\end{document}
  • What about the already provided answer? See also the comments below your question. – Dr. Manuel Kuehner May 7 '18 at 17:43
  • I updated the answer to explain why I posted it and how it differs from Stefan Pinnow's solution. – RobotRaven May 7 '18 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.