# pgfplots: How to get a legend of fill area?

I want to make a bar plot and put the average with error bar or an error behind it. So far I got it working, except I can't get the filled area to show up in the legend. Here is my working example

\documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{units,
fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ymajorgrids,
legend style={at={(0.5,-0.2)},
anchor=north,legend columns=-1},
ylabel={Si pro Zelle},
ymin=0,
ytick={0,2,...,16},
symbolic x coords={0, 5 Tage, 6 Tage, 7 Tage, 8 Tage, 1},
xtick=data,
x tick label style={rotate=45,
anchor=east},
]
error bars/.cd, y dir=both,y explicit,
]
table[x=x,y=y,y error=error,col sep=comma] {test.csv};
sharp plot,update limits=false,
] coordinates { (0,7.524) (1,7.524) }
node [above] at (6 Tage,7.524) {Average};
\addplot [transparent,name path=B,sharp plot, update limits=false,
] coordinates {(0,5.474)   (1,5.474) };
] coordinates { (0,9.574) (1,9.574) };
\addplot [red!10!white,area legend] fill between [
of=A and B];
\end{axis}
\end{tikzpicture}
test
\end{document}


I have tried the package manual and google but didn't find something. Does anyone have an idea how to get the fill area/color to show up in the legend?

I have two more minor issues:

1. Is there a more elegant way to let the average line start at the axis, not outside the plot?
2. How can I change only the color of the error bars?

\begin{filecontents*}{test.csv} x y error 5 Tage 4.031 0.457 6 Tage 6.205 0.065 7 Tage 14.275 0.869 8 Tage 5.585 0.229 \end{filecontents*}

• Please add filecontents and the settings that you use to your code to make it a real MWE. Sep 10, 2017 at 22:00
• Sep 11, 2017 at 3:36
• Sep 11, 2017 at 3:43
• Does my answer answer your questions or do you need further assistance? In the first case please consider upvoting it (with the upward pointing arrow to the left of it) and accepting it (by clicking on the checkmark ✓). In the later case, please edit your question accordingly. Thank you. Sep 14, 2017 at 5:04

You only made one small mistake to achieve what you want: \addlegendentry is not adding a legend entry to the previous \addplot command, but just collects entries. So only the order of these commands is of interest to produce the legend, not the place where you write the command(s). Said that, the picture of the "Error" entry in the legend actually shows the style of the "name path=B" \addplot which you defined as transparent.

To prove the said just add two more \addlegendentry commands and the "last one" will show (correctly) the style of the "fill between" \addplot command.

But this is not a real solution, because also if you just state empty arguments to the \addlegendentry command, these will occupy space in the legend. To prevent this, you can either use the \legend command instead (which I did in the shown solution below) or you can use legend entries in the axis options, where empty entries will not be added to the legend.

% used PGFPlots v1.15
\begin{filecontents*}{test.csv}
x,       y,       error
5 Tage,  4.031,   0.457
6 Tage,  6.205,   0.065
7 Tage,  14.275,  0.869
8 Tage,  5.585,   0.229
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymajorgrids,
legend style={
at={(0.5,-0.2)},
anchor=north,
legend columns=-1,
},
ylabel={Si pro Zelle},
ymin=0,
ytick distance=2,           % <-- replaced ytick' key
symbolic x coords={
0, 5 Tage, 6 Tage, 7 Tage, 8 Tage, 1},
xtick=data,
x tick label style={
rotate=45,
anchor=east,
},
axis on top,                % <-- added
]

ybar,
ybar legend,
blue,
fill=blue!30!white,
error bars/.cd,
y dir=both,
y explicit,
] table [
x=x,
y=y,
y error=error,
col sep=comma,
] {test.csv};

red,
line legend,
update limits=false,
] coordinates {
(0,7.524)
(1,7.524)
}
% specified relative positioning, rather than an absolute one
node [above,pos=0.5] {Average}
;

transparent,
name path=B,
update limits=false,
] coordinates {
(0,5.474)
(1,5.474)
};

transparent,
name path=A,
update limits=false,
] coordinates {
(0,9.574)
(1,9.574) }
;

red!10!white,
area legend,
] fill between [
of=A and B,
];

\legend{
Si-Pool,
Average,
,
,
Error,
}

\end{axis}
\end{tikzpicture}
\end{document}
`