如何减少 xbar 堆叠 pgfplots 图表中的垂直间距?

如何减少 xbar 堆叠 pgfplots 图表中的垂直间距?

考虑一下代码

\documentclass{article}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{filecontents}{test.csv}
name,a,b
name1,1000,1000000
name2,1000,1000000
\end{filecontents}
\begin{document}
\begin{tikzpicture}
  \pgfplotstableread[col sep=comma]{test.csv}\datatable
  \begin{axis}[
      xbar stacked,   % Stacked horizontal bars
      ytick=data,     % Use as many tick labels as y coordinates
      yticklabels from table={\datatable}{name},
      scaled x ticks=false,
    ]
    \addplot table [x=a,  meta=name, col sep=comma, y expr=\coordindex] {test.csv};
    \addplot table [x=b,  meta=name, col sep=comma, y expr=\coordindex] {test.csv};
    \legend{a,b}
  \end{axis}
\end{tikzpicture}
\end{document}

我明白了 在此处输入图片描述

我想要类似的东西

在此处输入图片描述

我如何让 pgf 生成这个图(而不是必须在 paint 中执行此操作?)

答案1

axis这可以通过调整'来实现height。有关详细信息,请查看代码中的注释。

% used PGFPlots v1.17
\begin{filecontents}{test.csv}
name,a,b
name1,1000,1000000
name2,1000,1000000
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % adjust the `height` and keep the `width`
        width=\axisdefaultwidth,
        height=3cm,
        % in addition something like this is needed so the bars don't
        % extend the axis limits and thus get cropped
        enlarge y limits={abs=0.5},
        % ---
        xbar stacked,
        ytick=data,
        % (you can state the file name here as well)
        yticklabels from table={test.csv}{name},
        scaled x ticks=false,
        % (this sets all labels to the same base
        %  compare the first two x labels (0 and 2*10^5) with your image)
        typeset ticklabels with strut,
        % (moved common stuff here so you don't need to repeat it)
        table/col sep=comma,
        table/y expr=\coordindex,
        table/meta=name,
    ]
        \addplot table [x=a] {test.csv};
        \addplot table [x=b] {test.csv};
        \legend{a,b}
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容