0

I have a plot where there are multiple legend entries. The colors rotate to different colors for 6 legend entries. After that, I have a repeat color. How do I specify a new color for the 7th entry (in MWE, it's 'eternity').

MWE:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    bar width=15pt,
    nodes near coords,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.20)},
      anchor=north,legend columns=-1},
    ylabel={\#participants},
    symbolic x coords={2005, tool2, tool3, tool4, 
        tool5, tool6, tool7},
    xtick=data,
    x tick label style={rotate=45,anchor=east},
    ]
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,2) 
  (tool3,2) (tool4,3) (tool5,0) (tool6,2) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,0) 
  (tool3,0) (tool4,3) (tool5,1) (tool6,1) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,6) (tool2,6)
  (tool3,8) (tool4,2) (tool5,6) (tool6,5) (tool7,6)};
\addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
\legend{\strut never, \strut rarely, \strut sometimes, \strut often, \strut always, \strut forever, \strut eternity}
\end{axis}
\end{tikzpicture}
\end{document}

Problem: The colors for entry 1 ('never') and entry 7 ('eternity') are the same. I would like all colors to be different.

enter image description here

UPDATE: I tried adding my own cycle list as shown in the new MWE and yet the colors remain the same.

MWE 2:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\pgfplotscreateplotcyclelist{Syphirint}{
   fill=black!80,draw=black,solid,mark=none\\
   fill=black!30,draw=black,solid,mark=none\\
   fill=black!30,draw=black,solid,mark=none\\
   fill=black!30,draw=black,solid,mark=none\\
   fill=black!30,draw=black,solid,mark=none\\
   fill=black!10,draw=black,solid,mark=none\\
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[cycle list name=Syphirint,
    ybar stacked,
    bar width=15pt,
    nodes near coords,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.20)},
      anchor=north,legend columns=-1},
    ylabel={\#participants},
    symbolic x coords={2005, tool2, tool3, tool4, 
        tool5, tool6, tool7},
    xtick=data,
    x tick label style={rotate=45,anchor=east},
    ]
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,2) 
  (tool3,2) (tool4,3) (tool5,0) (tool6,2) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,0) 
  (tool3,0) (tool4,3) (tool5,1) (tool6,1) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,6) (tool2,6)
  (tool3,8) (tool4,2) (tool5,6) (tool6,5) (tool7,6)};
\addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
  \addplot+[ybar] plot coordinates {(tool1,4) (tool2,2) 
  (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
\legend{\strut never, \strut rarely, \strut sometimes, \strut often, \strut always, \strut forever, \strut eternity}
\end{axis}
\end{tikzpicture}
\end{document}
Stefan Pinnow
  • 29,535
learnerX
  • 149
  • 8

1 Answers1

2

The key to make it work is to provide the cycle list/cycle list name after stating ybar/ybar stacked because they load the bar cycle list themselves and thus overwrite a previously stated cycle list/cycle list name.

(Please note that I have dramatically simplified your code as well. For details please have a look at the comments in the code.)

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
    \usepgfplotslibrary{colorbrewer}
    \pgfplotsset{
        compat=1.8,
        width=7cm,
        % (either create you own cycle list which has enough colors
        %  or use one that is already defined. I have chosen the latter
        %  from the `colorbrewer` library.
        %  Please note that this only *loads* the `cycle list`,
        %  but doesn't *activate* it yet.
        %  (Please note that this provided only a `draw` color,
        %   but not a `fill` color))
        cycle list/Set1-7,
    }
    % (I moved your data to a table which much more clearly
    %  I used the headings to provide the legend entries as well)
    \pgfplotstableread{
        x       never   rarely  sometimes   often   always  forever eternity
        tool1   0       0       6           4       4       4       4
        tool2   2       0       6           2       2       2       2
        tool3   2       0       8           0       0       0       0
        tool4   3       3       2           2       2       2       2
        tool5   0       1       6           3       3       3       3
        tool6   2       1       5           2       2       2       2
        tool7   0       0       6           4       4       4       4
    }{\tabledata}
    % (adapted from <https://tex.stackexchange.com/a/24023/95441>)
    \newcommand{\plottable}[1]{
        % get the number of columns in the table ...
        \pgfplotstablegetcolsof{#1}
        % ... and store it in a variable
        \pgfmathtruncatemacro\numberofcols{\pgfplotsretval-1}
        % cycle through the (y) columns
        \pgfplotsinvokeforeach{1,...,\numberofcols}{
            % plot the data
            \addplot table [x expr=\coordindex,y index=##1] {#1};
            % get the heading (name) of the currently processed column
            % and store it in a variable
            \pgfplotstablegetcolumnnamebyindex{##1}\of{#1}\to{\colname}
            % add the legend entry using the stored variable
            \addlegendentryexpanded{\colname}
        }
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        % activate the custom `cycle list`
        % It is important that this is done *after* stating
        % `ybar`/`ybar stacked`, because these styles incorporate loading
        % a default `cycle list` themselves. Thus a previously added
        % `cycle list` would be overwritten.
        cycle list name=Set1-7,
        bar width=15pt,
        enlargelimits=0.15,
        ylabel={\#participants},
        xtick=data,
        % (use the "x" column to provide the `xticklabels`
        %  so you don't have to repeat them manually by providing them
        %  as `symbolic coordinates`. But then numbers are required as
        %  input which we did above by stating `x expr=\coordindex`.)
        xticklabels from table={\tabledata}{x},
        x tick label style={rotate=45,anchor=east},
        nodes near coords,
        % (in case you don't create a `cycle list` *with* a fill color
        %  -- like in this case -- you could do this to
        %  add a fill color for all `\addplot` commands)
        every axis plot post/.append style={
            fill=.!75,
        },
        legend style={
            at={(0.5,-0.20)},
            anchor=north,
            legend columns=-1,
        },
    ]
        % (use above command to plot the bars)
        \plottable{\tabledata}
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535