为什么我的条形图有两种不同的样式?

为什么我的条形图有两种不同的样式?

我有两个条形图 - 一个使用误差线并从 绘制数据table,另一个则没有。据我所知,这是两者之间的唯一区别。为什么它们以不同的样式显示?为什么前者堆叠其条形图而不是并排放置?

代码:(pgfplotstat.sty直接来自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}

答案1

  • 您需要的ybar是轴环境,而不是选项\addplot

  • 如果您不使用\addplot+[...]与 TikZ 等效的样式,那么它将通过与 TikZ 等效的/.append style样式覆盖以前的样式。\addplot[]/.style

相关内容