堆叠 ybar 负值

堆叠 ybar 负值

我希望我的ybars 以正向而不是负向的方式堆叠。它适用于正值,但不适用于负值。在给定的示例中,ThirdCol 应该以负向方式堆叠。有什么堆叠方法吗?

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\pgfplotstableread{
Year    OneCol  SecCol  ThirdCol ForthCol
2005    10     50      -10     30  
2006    -40     60      -15     90
2007    -20     60      -15     60
}\datatable


\pgfplotsset{   width=5cm,
        compat=newest,
        height=5cm}
\usepgfplotslibrary{dateplot}

\begin{document}%
    \begin{tikzpicture}
        \begin{axis}[
            x tick label style={/pgf/number format/1000 sep=},
            ybar stacked,
            bar shift=0pt,
            enlarge x limits=0.15,
            bar width=1em,
            ymin=-100,
            legend style={
                at={(0.5,-0.1)},
                anchor=north,
                draw=none }
          ]
\addplot table [y index=1] {\datatable};
  \addplot table [y index=2] {\datatable};
   \addplot table [y index=3] {\datatable};
   \addplot table [y index=4] {\datatable};
  \legend{Factor1,Factor2,Factor3,Factor4}
  \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

在 PGFPlots v1.13 中,您的示例现在可以使用新键stack negative=separate,其中“separate”是默认值。完整代码如下,我只是对其进行了一点重新排序。手册第 99 页第 4.5.9 节中还添加了一个类似的示例。

请注意:由于某种原因,compat=newest它似乎不起作用,但实际上compat=1.13却起作用。

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{dateplot}
    \pgfplotsset{
%        compat=newest,  % <-- does not work; don't know why
        compat=1.13,     % <-- works as expected
        width=5cm,
        height=5cm,
    }
\begin{document}
    \begin{tikzpicture}
        \pgfplotstableread{
            Year    OneCol  SecCol  ThirdCol ForthCol
            2005    10     50      -10     30
            2006    -40     60      -15     90
            2007    -20     60      -15     60
        }\datatable
        \begin{axis}[
            x tick label style={
                /pgf/number format/1000 sep=,
            },
            ybar stacked,
            bar shift=0pt,
            enlarge x limits=0.15,
            bar width=1em,
            ymin=-100,
            legend style={
                at={(0.5,-0.1)},
                anchor=north,
                draw=none,
            },
        ]
            \addplot table [y index=1] {\datatable};
            \addplot table [y index=2] {\datatable};
            \addplot table [y index=3] {\datatable};
            \addplot table [y index=4] {\datatable};
            \legend{Factor1,Factor2,Factor3,Factor4}
        \end{axis}
    \end{tikzpicture}
\end{document}

上述代码中的图片

答案2

您无需重新构造输入数据,也可以通过 PGFPlotsrestrict y to domain*功能自动执行此操作。然后,您的示例将通过以下源代码绘制:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\pgfplotstableread{
Year    OneCol  SecCol  ThirdCol ForthCol
2005    10     50      -10     30  
2006    -40     60      -15     90
2007    -20     60      -15     60
}\datatable

\pgfplotsset{   width=5cm,
compat=newest,
height=5cm}
\usepgfplotslibrary{dateplot}

\begin{document}%
  \begin{tikzpicture}
    \begin{axis}[
      x tick label style={/pgf/number format/1000 sep=},
      ybar stacked,
      bar width=1em,
      ymin=-100,
      ymax=200,
      legend style={
      at={(0.5,-0.1)},
      anchor=north,
      draw=none },
      restrict y to domain*=:0, % negative values
      y=1pt,  % important workaround!
      ]
      \addplot table [y index=1] {\datatable};
      \addplot table [y index=2] {\datatable};
      \addplot table [y index=3] {\datatable};
      \addplot table [y index=4] {\datatable};
      \legend{Factor1,Factor2,Factor3,Factor4}
    \end{axis}
    \begin{axis}[
      x tick label style={/pgf/number format/1000 sep=},
      ybar stacked,
      bar width=1em,
      ymin=-100,
      ymax=200,
      legend style={
      at={(0.5,-0.1)},
      anchor=north,
      draw=none },
      restrict y to domain*=0:, % positive values
      y=1pt,  % important workaround!
      ]
      \addplot table [y index=1] {\datatable};
      \addplot table [y index=2] {\datatable};
      \addplot table [y index=3] {\datatable};
      \addplot table [y index=4] {\datatable};
      \legend{Factor1,Factor2,Factor3,Factor4}
    \end{axis}
  \end{tikzpicture}

\end{document}

这将产生以下图表: 结果图

答案3

好的,我已经完成了,但是这确实是一项拙劣的工作,如果有人有更好的编码方法,我将不胜感激。

我必须将正值与负值分开并将它们存储在两个不同的列中,然后通过声明两个轴来绘制它们,一个轴表示正值,另一个轴表示负值。

    \documentclass{standalone}
\usepackage{pgfplots,filecontents}
\usepgfplotslibrary{units}
\pgfplotsset{width=16cm,compat=newest}

\begin{filecontents}{total.dos}
Yr  C1  C2      C3      C4  C5      C6  C7  C8      C9  C10
2003    0   -0.11864    0.087399    0   0.012936    0   0   -0.097901   0   -0.090281   
2004    0   -0.73465    0.56238 0   0.044703    0   0   -0.04678    0.051024    0   
2005    0.5 0       0.64271 0   0.071566    0   0   -0.25326    0.026343    0   
2006    0   -0.9268 0.74394 0   0.10508 0   0   -0.26348    0.058272    0   

\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[const plot,
  stack plots=y,
  xlabel={Energy},
  x unit={eV},
  ylabel={Intensity},
  no markers,
  ymin=-3,
  ymax=3
]
  \addplot [fill=red,draw=none] table [x index=0,y index=3] {total.dos}\closedcycle;
  \addplot [fill=blue,draw=none,fill opacity=0.25] table [x index=0,y index=1] {total.dos}\closedcycle;
  \addplot [fill=green,draw=none,fill opacity=0.25] table [x index=0,y index=5] {total.dos}\closedcycle;
 \addplot [fill=yellow,draw=none,fill opacity=0.5] table [x index=0,y index=7] {total.dos}\closedcycle;
\end{axis}
\begin{axis}[const plot,
  stack plots=y,
  no markers,
  ymin=-3,
  ymax=3,
  xtick=\empty,
  ytick=\empty,
  axis x line=none,
  axis y line=none
]
  \addplot [fill=red,draw=none] table [x index=0, y index=4] {total.dos}\closedcycle;
  \addplot [fill=blue,draw=none,fill opacity=0.25] table [x index=0,y index=2] {total.dos}\closedcycle;
 \addplot [fill=green,draw=none,fill opacity=0.25] table [x index=0,y index=6] {total.dos}\closedcycle;
\addplot [fill=yellow,draw=none,fill opacity=0.5] table [x index=0,y index=8] {total.dos}\closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

一个更简单的解决方案是拆分域并将横坐标偏移一点点。

\pgfplotstableread{
Year       OneCol  SecCol  ThirdCol ForthCol
2005           10      50         0       30  
2005.0001       0       0       -10        0  
2006            0      60         0       90
2006.0001     -40       0       -15        0
2007            0      60         0       60
2007.0001     -20       0       -15        0
}\datatable

相关内容