堆叠的 ybar 图会创建额外的空 y 项

堆叠的 ybar 图会创建额外的空 y 项

我有这个代码:

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{
  compat=1.16,
  table/col sep=comma}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
Type, fillA, fillC, preAllocA, preAllocC
off, 10, 20, 0, 0
tree, 4, 5, 6, 4
computed, 6, 5, 4, 8
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ybar stacked,
    symbolic x coords={off, tree, computed},
    x tick label style={rotate=45,anchor=east}]
    \legend{FillA, FillC, preAllocA, preAllocC}

    \addplot+ table[y=fillA]{data.csv};
    \addplot+ table[y=fillC]{data.csv};
    \addplot+ table[y=preAllocA]{data.csv};
    \addplot+ table[y=preAllocC]{data.csv};
  \end{axis}
\end{tikzpicture}

\end{document}

该代码基本上可以工作,但是为树和计算创建了一个额外的类别(y 条目),如下所示: additional y entry

这是为什么?我该如何预防?

还有一个小问题:有没有办法摆脱符号 x 坐标并让 pgfplots 从文件的第一列获取坐标?

再次感谢!

答案1

防止 pgfplots 添加更多刻度的最简单方法可能是添加xtick=data。例如,请参阅手册第 97 页上的示例。

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{
  compat=1.16,
  table/col sep=comma}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
Type, fillA, fillC, preAllocA, preAllocC
off, 10, 20, 0, 0
tree, 4, 5, 6, 4
computed, 6, 5, 4, 8
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ybar stacked,
    symbolic x coords={off, tree, computed},
    xtick=data,
    x tick label style={rotate=45,anchor=east}]
    \legend{FillA, FillC, preAllocA, preAllocC}

    \addplot table[y=fillA]{data.csv};
    \addplot+ table[y=fillC]{data.csv};
    \addplot+ table[y=preAllocA]{data.csv};
    \addplot+ table[y=preAllocC]{data.csv};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

答案2

回答最后一部分,您可以设置xtick={0,1,2}(我首先尝试了xtick distance=1更好的方法,但效果不太好,至少在 Overleaf 上不行),然后添加xticklabels from table={data.csv}{Type}以从文件中获取刻度标签。最后,在图中使用它x expr来设置 x 坐标,例如\addplot+ table[x expr=\coordindex, y=fillA]{data.csv};

enter image description here

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{
  compat=1.14,% overleaf was still on 1.14, so I changed the compat-setting
  table/col sep=comma
}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
Type, fillA, fillC, preAllocA, preAllocC
off, 10, 20, 0, 0
tree, 4, 5, 6, 4
computed, 6, 5, 4, 8
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ybar stacked,
    xtick={0,1,2}, % set explicit tick positions
    xticklabels from table={data.csv}{Type}, % get ticklabels from Type column
    x tick label style={rotate=45,anchor=north east}, % change east to north east
    % just a quick suggestion for the legend layout
    legend columns=2,
    legend cell align=left,
    legend transposed
    ]
    \legend{FillA, FillC, preAllocA, preAllocC}

    % \coordindex corresponds to row number in table, counting from 0
    \addplot+ table[x expr=\coordindex, y=fillA]{data.csv};
    \addplot+ table[x expr=\coordindex, y=fillC]{data.csv};
    \addplot+ table[x expr=\coordindex, y=preAllocA]{data.csv};
    \addplot+ table[x expr=\coordindex, y=preAllocC]{data.csv};
  \end{axis}
\end{tikzpicture}

\end{document}

相关内容