adding error bars to bar plot

2,746

You were on the right track. The biggest "mistake" you made was to use ybar interval which broke a lot. I present a solution quite near what you want and I guess you can do the rest on your own.

What I didn't understand is what you mean with giving the error bars a legend entry. Could you further elaborate that?

For more detail on how my solution works, please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % (use this `compat` level or higher to make use of the "advanced" axis
    %  label positioning)
    \pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar,
        bar width=7pt,
        % (changed from `xtick`)
        xtick distance=1,
        xlabel=x values,
        ylabel=y values,
        % (changed to an absolute value)
        enlarge x limits={abs=0.5},
        % ---------------------------------------------------------------------
        % changes to get what you want
        % ---------------------------------------------------------------------
        ymin=0,
        scaled ticks=false,
        % remove the `xticks`
        xtick style={
            /pgfplots/major tick length=0pt,
        },
%        % `ybar interval` is not what you want here I guess, because then e.g.
%        % the last bar is not drawn. To simulate that ...
%        % ... add `extra x ticks` between the `xticks` ...
%        extra x ticks={1.5,...,8.5},
%        % ... without stating any label ...
%        extra x tick labels={},
%        % ... and show the major gridlines
%        extra x tick style={
%            xmajorgrids,
%        },
    ]
        \addplot coordinates {
            (1,4473.317111) (2,7394.47646)  (3,6886.499957)
            (4,9836.193336) (5,8098.401475) (6,8308.855452)
            (7,9983.205801) (8,7617.506648) (9,9617.462814)
        };

        \addplot+ [
            error bars/.cd,
                y dir=both,
                % (changed from `y explicit` so the error bars are (clearly) visible
                y explicit relative,
        ] coordinates {
            (1,4342.7395) +- (0,0.05)
            (2,7381.3423) +- (0,0.05)
            (3,6837.375) +- (0,0.05)
            (4,9964.5747) +- (0,0.05)
            (5,7624.083) +- (0,0.05)
            (6,7843.0192) +- (0,0.05)
            (7,9665.1374) +- (0,0.05)
            (8,7266.0779) +- (0,0.05)
            (9,9393.7355) +- (0,0.05)
        };

        \legend{
            x data,
            y values,
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Share:
2,746

Related videos on Youtube

user151562
Author by

user151562

Updated on January 19, 2020

Comments

  • user151562
    user151562 almost 4 years

    I have this figure in Excel that I want to replicate in LaTeX.

    enter image description here

    So far, I have these.

    enter image description here

    Can anyone help me to get as close as possible to the first figure please? Also, I want to add a legend to the error bars to let the reader know its magnitude.

    My MWE:

    \documentclass[11pt,paper=a4,BCOR=15mm,bibliography=totoc,DIV=9,final,headings=optiontohead,
    listof=chaptergapsmall,listof=totoc,numbers=noenddot,openright,parskip=half,titlepage,twoside,]{scrreprt}
    \usepackage[ngerman,english,]{babel} 
    \usepackage[onehalfspacing]{setspace}       
    \usepackage{graphicx}
    \usepackage{tikz,pgfplots}
    \usetikzlibrary{intersections, calc}
    \usepgfplotslibrary{units}
    
    
    \begin{document}
    
    \begin{figure}[h!]
    \centering
    \begin{tikzpicture}
    [scale=1.23]
    \begin{axis}[
          ybar,
            bar width=7pt,
            xtick={1,...,9},
        ylabel= y values,
        xlabel=y values,
        enlargelimits=0.05,
        ybar interval=0.7,
        %       x unit=-,
        %       y unit=\si{\frac{kg}{h}}, 
        ]   
    \addplot
        coordinates {(1,4473.317111) (2,7394.47646) (3,6886.499957) 
        (4,9836.193336) (5,8098.401475) (6,8308.855452) (7,9983.205801) 
        (8,7617.506648) (9,9617.462814)};
    \addplot+[error bars/.cd, y dir=both,y explicit]
        coordinates {(1,4342.7395) +- (0,0.05)
                    (2,7381.3423) +- (0,0.05)
                    (3,6837.375) +- (0,0.05)
                    (4,9964.5747) +- (0,0.05)
                    (5,7624.083) +- (0,0.05)
                    (6,7843.0192) +- (0,0.05)
                    (7,9665.1374) +- (0,0.05)
                    (8,7266.0779) +- (0,0.05)
                    (9,9393.7355) +- (0,0.05)
                    };
    \legend{x data,y values}
    \end{axis}
    \end{tikzpicture}
    \caption{Figure Caption}
    \label{fig:plot1}
    \end{figure}
    
    \end{document}
    
  • user151562
    user151562 almost 5 years
    Really nice explanation and optimal solution. What I meant with a legend is that I want to show that the error bars represent 5% error, and it would be ideal to put ¨+-5%¨ below "y values" (as part of the legend)
  • Stefan Pinnow
    Stefan Pinnow almost 5 years
    One could do that, but would be rather uncommon. I suggest that you put that information inside the \caption.