更改高级条形图的颜色(pgfplotstable)

更改高级条形图的颜色(pgfplotstable)

根据这个精彩的答案,我制作了一个堆叠和聚集图:https://tex.stackexchange.com/a/55559/93892。但是,我想改变条形的颜色。最重要的是,我想将红色和蓝色改为灰色/黑色。每隔一列进行区分(以区分初始列和主列)可能很有趣,但这并不是至关重要的。

我尝试了几种方法,但都没有成功。我认为这应该相当简单,所以如果你们中有人能立即明白如何做到这一点,我将不胜感激。

\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

这是命令fill=中的选项\addplot(您在代码中忘记了它 - 根据链接的答案,我猜到了您试图绘制的内容)。灰色阴影可以通过以下方式实现black=!50(提供介于黑色和白色之间的阴影)、black=!10(提供从黑色到白色的 10% 的阴影)等等。以下是示例:

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

将代码中的条目替换\addplot为上面的条目,并根据需要调整数字。

相关内容