I have a stacked bar chart with error bars that sometimes dips below the top part, which hides the remaining part of the error bar. (see both blue and red for x=5 and the red for all others). I want to show the error bars entirely. I've tried various clipping options, but it didn't help with my problem.
\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{patterns}
\pgfplotsset{
discard if/.style 2 args={
x filter/.code={
\edef\tempa{\thisrow{#1}}
\edef\tempb{#2}
\ifx\tempa\tempb
\def\pgfmathresult{inf}
\fi
}
}
}
\makeatletter
\newcommand\resetstackedplotsfive{
\makeatletter
\pgfplots@stacked@isfirstplottrue
\makeatother
\addplot [forget plot,draw=none] coordinates{(1,0) (2,0) (3,0) (4,0) (5,0)};
}
\begin{document}
\begin{filecontents}{data.csv}
X N Name Activation Inclusion Min Max
1 3 CR 3.73 0.85 0.50 0.23
2 3 LR 3.93 0.04 0.97 -0.13
4 5 CR 4.54 1.25 2.21 0.78
5 5 LR 4.36 0.18 0.75 0.39
7 7 CR 4.66 1.84 0.84 0.92
8 7 LR 4.82 0.32 0.51 0.53
10 9 CR 5.19 2.71 1.27 0.8
11 9 LR 5.62 0.53 0.87 0.91
13 11 CR 5.69 3.53 1.78 1.57
14 11 LR 6.00 0.71 1.16 0.71
\end{filecontents}
\def\datafile{data.csv}
\begin{tikzpicture}
\begin{axis}[
axis lines*=left,
ymajorgrids,
width=.8\linewidth, height=5cm,
ymin=0,
ytick={0,2,4,6,8,10},
ybar stacked,
bar width=14.7pt,
xtick={1.5,4.5,7.5,10.5,13.5},
xticklabels={3,5,7,9,11},
]
\addplot
[fill=blue!60!white, discard if={Name}{LR}]
table [x=X, y=Activation] {\datafile};
\addplot
[fill=blue!20!white, discard if={Name}{LR}]
plot [error bars/.cd, y dir=both, y explicit]
table [x=X, y=Inclusion, y error plus=Max, y error minus=Min] {\datafile};
\resetstackedplotsfive
\addplot
[fill=red!60!white, discard if={Name}{CR}]
table [x=X, y=Activation] {\datafile};
\addplot
[fill=red!20!white, discard if={Name}{CR}]
plot [error bars/.cd, y dir=both, y explicit]
table [x=X, y=Inclusion, y error plus=Max, y error minus=Min] {\datafile};
\end{axis}
\end{tikzpicture}
\end{document}
This produces the following:
Best Answer
If you don't have any error bars on the bottom part you can elevate the top stack to another layer above
main
, otherwise you can modify the error bars draw code by explicitly moving to another layer.For some reason, the regular
error bar style={/pgfplots/on axis=axis foreground}
doesn't grant this elevation.