如何使用 pgfplots 自动拟合条形图中的多个条形?

如何使用 pgfplots 自动拟合条形图中的多个条形?

我正在尝试使用 PGFPlots 来显示大量的条形图,但是我遇到了它会切断最后一个条形图的问题。

梅威瑟:

\documentclass[crop,tikz]{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.13}

\begin{document}

\pgfplotstableread{
row label runtime
0 1 5264
1 2 2971
2 4 1866
3 8 1384
4 16 1205
5 32 1144
6 64 1109
7 128 1095
8 256 1108
9 512 1178
10 1024 1335
11 2048 1667
12 4096 1588
13 8192 2843
14 16384 5541
15 32768 10985
16 65536 21873
17 131072 43687
18 262144 87064
19 524288 174119
}\mytable


\begin{tikzpicture}
  \begin{axis}[
        ylabel={runtime (${\mu}s$)},
        xticklabels from table={\mytable}{label},
        xtick=data,
        ymin=0,
        ymax=10000,
        ybar,
        xticklabel style={
          anchor=north west,
          align=left,
          rotate=-45,
          font=\tiny,
          inner sep=1pt,
        },
]
\addplot+ [] table [y=runtime, x expr=\coordindex] {\mytable};
\end{axis}
\end{tikzpicture}

\end{document}

结果:

瓦特0

通过在轴选项中添加以下内容,我可以使所有条形图都适合。但我需要手动调整bar width(和xmax

xmin=-1,
xmax=20,
bar width=7pt,

WATT1

有办法处理吗许多自动在条形图中绘制条形图吗?

答案1

如果我没记错的话,手册上目前有可以自动完成。但是从 v1.7 开始,很有可能采用一种良好的半自动化方法,您只需进行修改xmax(在本例中)即可使其独立于其他内容工作。

有关更多详细信息,请查看代码中的注释。

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    % use this `compat' level or higher to be able to use absolute values for
    % the `bar width'
    \pgfplotsset{compat=1.7}
\begin{document}

\pgfplotstableread{
row label runtime
0 1 5264
1 2 2971
2 4 1866
3 8 1384
4 16 1205
5 32 1144
6 64 1109
7 128 1095
8 256 1108
9 512 1178
10 1024 1335
11 2048 1667
12 4096 1588
13 8192 2843
14 16384 5541
15 32768 10985
16 65536 21873
17 131072 43687
18 262144 87064
19 524288 174119
}\mytable
\begin{tikzpicture}
  \begin{axis}[
        ylabel={runtime (${\mu}s$)},
        xticklabels from table={\mytable}{label},
        xtick=data,
        % ---------------------------------------------------------------------
        % to make the bar plot look good ...
        % ----------
        % ... provide the used axis limits ...
        % (using `\coordindex' means `xmin' always starts from 0)
        xmin=0,
        xmax=19,  % <-- this needs to be adjusted manually each time
        % provide a suitable `bar width' in absolute values.
        % This makes the plot independent of axis `width'
        % (this also only have to set once, because the increment is always 1)
        bar width=0.8,
        % ... and enlarge them by an absolute value which satisfies your needs
        % (this should be larger than half of the `bar width' so that the
        %  bars at the axis limits are fully shown)
        enlarge x limits={abs=0.6},
        % ---------------------------------------------------------------------
        ymin=0,
        ymax=10000,
        ybar,
        xticklabel style={
          anchor=north west,
          align=left,
          rotate=-45,
          font=\tiny,
          inner sep=1pt,
        },
]
    \addplot table [y=runtime, x expr=\coordindex] {\mytable};
\end{axis}
\end{tikzpicture}
\end{document}

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

相关内容