1

I'm trying to make a stacked bar chart with two bars next to each other with different colors. I've based my code on discarding \addplot commands similar to this question, but even though I'm discarding an \addplot it seems to remember the height of the previous bar (due to stacking presumably). I want all bars to start at y=0.

\documentclass{standalone}
\usepackage{filecontents} 
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

\begin{document}

\begin{filecontents}{data.csv}
X   Alpha   Name    Normal  Spike   Min     Max
1   5       CR      2.42    3.19    0.92    0.98
2   5       LR      2.48    0.80    0.59    0.47
4   10      CR      2.03    3.30    1.76    2.21
5   10      LR      2.10    0.92    0.53    0.62
7   15      CR      2.03    2.88    1.60    1.28
8   15      LR      2.05    1.12    0.68    0.69
\end{filecontents}

\pgfplotstablegetrowsof{data.csv}
\edef\numberofrows{\pgfplotsretval}

\begin{tikzpicture}
\begin{axis}[
    width=10cm,height=6cm,
    ybar stacked,
    xtick={1,2,4,5,7,8},
    xticklabels from table={data.csv}{X},
]

\addplot [draw,fill=orange,discard if={Name}{LR}] table [x=X, y=Normal] {data.csv};
\addplot [draw,fill=orange!20,discard if={Name}{LR}] table [x=X, y=Spike] {data.csv};

\addplot [draw,fill=green,discard if={Name}{CR}] table [x=X, y=Normal] {data.csv};
\addplot [draw,fill=green!20,discard if={Name}{CR}] table [x=X, y=Spike] {data.csv};

\end{axis}
\end{tikzpicture}

\end{document}
hein
  • 73

0 Answers0