2

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}
\pgfplotsset{compat=newest}
\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}, 
]
\addplot[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};
\addlegendentry{Si-Pool}
\addplot [red,line legend,
    sharp plot,update limits=false,
    ] coordinates { (0,7.524) (1,7.524) }
    node [above] at (6 Tage,7.524) {Average};
\addlegendentry{Average}    
\addplot [transparent,name path=B,sharp plot, update limits=false,
    ] coordinates {(0,5.474)   (1,5.474) };
\addplot [transparent,name path=A,sharp plot,update limits=false,
    ] coordinates { (0,9.574) (1,9.574) };
\addplot [red!10!white,area legend] fill between [
    of=A and B];
\addlegendentry{Error}
\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?

Thanks in advance!

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*}

Plot

4
2

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
    ]

        \addplot [
            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};

        \addplot [
            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}
        ;

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

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

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

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

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

image showing the result of above code

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.