0

I have made a stacked and clustered diagram, based on this brilliant answer: https://tex.stackexchange.com/a/55559/93892. However, I would like to change the colors of the bars. The most important thing is I'd like to change from red and blue to shades of gray/black. It might be interesting having a distinction between every second column (to distinguish between Initial and Main), but this is not crucial.

I have tried a few approaches with no success. I suspect this should be quite easy, so if any of you immediately see how it's done some input would be highly appreciated.

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}


\newcounter{groupcount}
\pgfplotsset{
draw group line/.style n args={5}{
    after end axis/.append code={
        \setcounter{groupcount}{0}
        \pgfplotstableforeachcolumnelement{#1}\of\datatable\as\cell{%
            \def\temp{#2}
            \ifx\temp\cell
                \ifnum\thegroupcount=0
                    \stepcounter{groupcount}
                    \pgfplotstablegetelem{\pgfplotstablerow}{X}\of\datatable
                    \coordinate [yshift=#4] (startgroup) at (axis cs:\pgfplotsretval,0);
                \else
                    \pgfplotstablegetelem{\pgfplotstablerow}{X}\of\datatable
                    \coordinate [yshift=#4] (endgroup) at (axis cs:\pgfplotsretval,0);
                \fi
            \else
                \ifnum\thegroupcount=1
                    \setcounter{groupcount}{0}
                    \draw [
                        shorten >=-#5,
                        shorten <=-#5
                    ] (startgroup) -- node [anchor=base, yshift=0.5ex] {#3} (endgroup);
                \fi
            \fi
        }
        \ifnum\thegroupcount=1
                    \setcounter{groupcount}{0}
                    \draw [
                        shorten >=-#5,
                        shorten <=-#5
                    ] (startgroup) -- node [anchor=base, yshift=0.5ex] {#3} (endgroup);
        \fi
        }
    }
}

\begin{document}

\begin{figure}
\begin{center}
\begin{tikzpicture}
\pgfplotstableread{
X   Md  Ts  Name        Remaining      Removed
1   Initial 30min   Constraints     30182    5158
2   Initial 30min   Variables   34018      86
4   Main 30min   Constraints      3257      2787
5   Main 30min   Variables     5615    373
8   Initial 15min   Constraints   59924      9436
9   Initial 15min   Variables     67406      172
11  Main 15min   Constraints   7582      4100
12  Main 15min   Variables      11027    841
15  Initial 5min   Constraints     176321    29119
16  Initial 5min   Variables   200985    516
18  Main 5min   Constraints     20398     10696
19  Main 5min   Variables   31350      2068  
}\datatable

\begin{axis}[
scaled ticks=false,
tick label style={/pgf/number format/fixed},
axis lines*=left, ymajorgrids,
ymax = 220000,
width=12cm, height=15cm,
ymin=0,
ybar stacked,
bar width=11pt,
xtick=data,
xticklabels from table={\datatable}{Name},
xticklabel style={rotate=90,xshift=-7ex,anchor=mid east},
legend style={at={(0.04,0.93)},anchor=west},
draw group line={Ts}{30min}{30min}{-4ex}{4pt},
draw group line={Ts}{15min}{15min}{-4ex}{4pt},
draw group line={Ts}{5min}{5min}{-4ex}{4pt},
draw group line={Md}{Initial}{Initial}{-7ex}{4pt},
draw group line={Md}{Main}{Main}{-7ex}{4pt},
after end axis/.append code={
    \path [anchor=base east, yshift=0.5ex]
        (rel axis cs:0,0) node [yshift=-7ex] {Model}
        (rel axis cs:0,0) node [yshift=-4ex] {Time slot size};
}]

\addplot table [x=X, y=Remaining] {\datatable}; \addlegendentry{Remaining}
\addplot table [x=X, y=Removed] {\datatable}; \addlegendentry{Removed}

\end{axis}

\end{tikzpicture}
\end{center}
\end{figure}
\end{document}
1

This is the fill= option in the \addplot command (which you had forgotten in your code - I guessed what you were trying to draw based on the linked answer). The shades of gray are possible with black=!50 (which gives a shade halfway between black and white), black=!10 (which gives a shade 10% from black to white), and so on. Here's an example:

\addplot[fill=black!10] table [x=X, y=Remaining] {\datatable}; \addlegendentry{Remaining}
\addplot[fill=black!60] table [x=X, y=Removed] {\datatable}; \addlegendentry{Removed}

Replace the \addplot entries in your code with the ones above, and adjust the numbers as necessary.

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.