1

I am trying to use PGFPlots to display a large amount of bars, and I run into the problem that it cuts off the last of them.

MWE:

\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}

Result:

wat0

By adding the following to the axis options, I can make all the bars fit. But I need to manually adjust bar width (and xmax)

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

wat1

Is there a way to handle many bars in a barplot automatically?

2

When I remember correctly from the manual there currently is no possibility to do that automatically. But since v1.7 there are good possibilities to have a good semi-automatic approach where you only have to modify xmax (in this case) to make it work independent of other stuff.

For more details please have a look at the comments in the code.

% 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}

image showing the result of above code

2
  • Ahh, that's nice! Can you explain the difference of setting enlarge x limits to {abs=0.6} and 0.6? I can see that the whitespace on each side becomes larger when not using abs.
    – RasmusWL
    Feb 18 '17 at 18:08
  • 1
    Sure. If you only state the number as value this equals to rel=<value> which means for your value: Add 60% of the x interval (on each side). Feb 18 '17 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.