4

I have two bar plots—one uses error bars and draws its data from a table, the other does not. As far as I can tell, this is the only difference between the two. Why are they displayed in different styles? Why does the former stack its bars rather than setting them side-by-side?

Code: (pgfplotstat.sty directly from Automatic calculation of error in pgfplots)

\documentclass[border=1cm]{standalone}
\usepackage{filecontents}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% BEGIN PACKAGE SETUP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    \begin{filecontents}{pgfplotstat.sty}
    \newcommand{\calcrowmean}{
        \def\rowmean{0}
        \pgfmathparse{\pgfkeysvalueof{/pgfplots/table/summary statistics/end index}-\pgfkeysvalueof{/pgfplots/table/summary statistics/start index}+1}
        \edef\numberofcols{\pgfmathresult}
                % ... loop over all columns, summing up the elements
        \pgfplotsforeachungrouped \col in {\pgfkeysvalueof{/pgfplots/table/summary statistics/start index},...,\pgfkeysvalueof{/pgfplots/table/summary statistics/end index}}{
            \pgfmathparse{\rowmean+\thisrowno{\col}/\numberofcols}
            \edef\rowmean{\pgfmathresult}
        }
    }
    \newcommand{\calcstddev}{
        \def\rowstddev{0}
        \calcrowmean
        \pgfplotsforeachungrouped \col in {\pgfkeysvalueof{/pgfplots/table/summary statistics/start index},...,\pgfkeysvalueof{/pgfplots/table/summary statistics/end index}}{
            \pgfmathparse{\rowstddev+(\thisrowno{\col}-\rowmean)^2/(\numberofcols-1)}
            \edef\rowstddev{\pgfmathresult}
        }
        \pgfmathparse{sqrt(\rowstddev)}
    }
    \newcommand{\calcstderror}{
        \calcrowmean
        \calcstddev
        \pgfmathparse{sqrt(\rowstddev)/sqrt(\numberofcols)}
    }

    \pgfplotstableset{
        summary statistics/start index/.initial=1,
        summary statistics/end index/.initial=4,
        create col/mean/.style={
            /pgfplots/table/create col/assign/.code={% In each row ... 
                \calcrowmean
                \pgfkeyslet{/pgfplots/table/create col/next content}\rowmean
            }
        },
        create col/standard deviation/.style={
            /pgfplots/table/create col/assign/.code={% In each row ... 
                \calcstddev
                \pgfkeyslet{/pgfplots/table/create col/next content}\pgfmathresult
            }
        },
        create col/standard error/.style={
            create col/assign/.code={% In each row ... 
                \calcstderror
                \pgfkeyslet{/pgfplots/table/create col/next content}\pgfmathresult
            }
        }
    }
    \end{filecontents}


    \begin{filecontents}{setup.sty}
    \usepackage{pgfplots,
      pgfplotstable,
      booktabs,
      colortbl,
      pgfplotstat,
      amsmath}
    \pgfplotsset{compat=1.7}

    \pgfplotstableset{
      summary statistics/start index/.initial=1,
      summary statistics/end index/.initial=3,
      create on use/mean/.style={create col/mean},
      create on use/stddev/.style={create col/standard deviation},
      create on use/stderror/.style={create col/standard error},
      every head row/.style={
        before row={\toprule},
        after row={\midrule}},
      every last row/.style={
        after row={\bottomrule}},
      every even row/.style={
        before row={\rowcolor[gray]{0.9}}},
      columns/.style={
        dec sep align}}
    \end{filecontents}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% END PACKAGE SETUP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{setup}

\begin{filecontents}{fish-8.9mgL.dat}
fishy measure1 measure2 measure3
    1       92       98       90
    2       66       86       80
    3       78       90       86
    4       96       80       84
    5       82       78       86
\end{filecontents}
\begin{filecontents}{fish-3.9mgL.dat}
fishy measure1 measure2 measure3
    1      148      138      148
    2      120      136      138
    3      124      144      150
    4      132      144      130
    5      124      112      138
\end{filecontents}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    title={Comparison of Opercullum Openings
      with Varying Oxygen Content},
    xlabel=Fish,
    ylabel=Opercullum openings per minute]

    \addplot[ybar,
              error bars/y dir=both,
              error bars/y explicit]
      table[x=fishy,
            y=mean,
      y error=stderror]
        {fish-8.9mgL.dat};

    \addplot[ybar,
              error bars/y dir=both,
              error bars/y explicit]
      table[x=fishy,
            y=mean,
      y error=stderror]
        {fish-3.9mgL.dat};
 \end{axis}
\end{tikzpicture}

% Straight from `texdoc pgfplots` page 61 [2012-10-26]
\begin{tikzpicture}
  \begin{axis}[
    x tick label style={
      /pgf/number format/1000 sep=},
    ylabel=Population,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ybar,
    bar width=7pt,
    ]
    \addplot
      coordinates {(1930,50e6) (1940,33e6)
        (1950,40e6) (1960,50e6) (1970,70e6)};
    \addplot
      coordinates {(1930,38e6) (1940,42e6)
        (1950,43e6) (1960,45e6) (1970,65e6)};
    \addplot
      coordinates {(1930,15e6) (1940,12e6)
        (1950,13e6) (1960,25e6) (1970,35e6)};
    \addplot[red,sharp plot,update limits=false]
      coordinates {(1910,4.3e7) (1990,4.3e7)}
      node[above] at (axis cs:1950,4.3e7) {Houses};
    \legend{Far,Near,Here,Annot}
  \end{axis}
\end{tikzpicture}

\end{document}
Sean Allred
  • 27,421

1 Answers1

4
  • You need ybar in the axis environment not in the \addplot options.

  • If you don't use \addplot+[...] which is the TikZ equivalent of /.append style then it overwrites the previous styles via \addplot[] which is TikZ equivalent of /.style.

percusse
  • 157,807
  • ybar in the axis environment did the trick—makes perfect sense, too. Although I did try both \addplot and \addplot+ with no discernible effect; I'm assuming the PGF manual goes over this difference (and its consequences) in detail? – Sean Allred Sep 01 '13 at 19:32
  • redacted: removing + will remove the fill color in this case. Thanks, percusse! – Sean Allred Sep 01 '13 at 19:33
  • @SeanAllred My pleasure – percusse Sep 01 '13 at 19:44