PGFplots – Fill PGFplot Bars with Colorbrewer

colorpgfplotstikz-pgf

I want to use colorbrewer in my project since it provides color cycle lists but I struggle to use them in the right way I think.
Please have a look at my MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer}
\pgfplotsset{cycle list/Dark2-8}

\begin{document}

\begin{figure}[h!]
    \pgfplotstableread[row sep=\\,col sep=&]{
            abc & 1 \\
            def & 5 \\
    }\datapoints
    \begin{tikzpicture}
        \begin{axis}[
            % general appearance
            ybar,
            cycle multi list=Dark2-8,
            % x axis
            symbolic x coords={def},
            xtick=data,
            xticklabel style={align=center},
            % y axis
            bar width=0.2cm,
            % legend
            area legend,
            legend entries={1},
            legend pos=north west
        ]
            \addplot table[y=1, x=abc]{\datapoints};
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

My Problem is, that the bar is not filled and I don't really know how to achieve this without adding a manual fill for each addplot.

By the way, if I try to add fill=Dark2-8-1 to the addplot options I get the error ! Package xcolor Error: Undefined color Dark2-8-1.

Can anybody point out where I went wrong?

Best Answer

You can add every axis plot/.append style={fill} to solve the fill problem. Also, you can call a specific color from the cycle list by using Dark2-<color letter>, if the cycle list initialized is Dark2-8 a color from the list is Dark2-D.

Code

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer}
\pgfplotsset{cycle list/Dark2-8}

\begin{document}
    \pgfplotstableread{
        x   y
        a   5
        b   6
        c   4
    }\mytable

    \begin{tikzpicture}
        \begin{axis}[
            ybar,
            symbolic x coords={a,b,c},
            xtick=data,
            every axis plot/.append style={fill},
            cycle list name=Dark2-8
        ]
            \addplot table [y=y, x=x]{\mytable};
            \addplot table [y=y, x=x]{\mytable};
            \addplot[fill=Dark2-D,draw=Dark2-D] coordinates {(a,7) (b,1) (c,2)};
        \end{axis}
    \end{tikzpicture}
\end{document}

Another way, recommended by the PGFPlots manual, to get a particular color from an initialized color scheme, is by using the key index of colormap as follows:

\addplot[index of colormap=3 of Dark2-8] coordinates {(a,7) (b,1) (c,2)};

Result

enter image description here