2

I need a bar plot (containing multiple \addplot) with following requirements:

  1. ticks between x labels (and, in this respect, my question differs from Adjusting width of ybar interval separator to width of histogram bars),
  2. non zero space between bars of adjacent x coordinates,
  3. zero space between bars corresponding to the same x coordinate.

I could manage the requirements:

  1. by using a ybar interval plot,
  2. by using a value <1 for the ybar interval option.

as shown by the following MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
%
\pgfplotsset{compat=1.11}
%
\begin{filecontents}{data.txt}
A B C D
0 13 9 19
1 0 1 5.5
2 0 4 4
3 1 3 14.5
4 3 8 6
5 1 8 6.5
6 2 5 5.5
7 0 7 14
8 8 14 6
9 0 5 12.5
10 0 14 17.5
\end{filecontents}
%
\begin{document}
\begin{tikzpicture}
  \begin{axis}[%
    ybar interval=0.5,%
    width=\textwidth%
    ]
    \addplot table[x=A,y=B]{data.txt};
    \addplot table[x=A,y=C]{data.txt};
    \addplot table[x=A,y=D]{data.txt};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

But I don't know how to remove the horizontal space between the bars of a same "interval". How could I achieve this?

1

Here I present a solution without ybar interval. For details on how it works, please have a look at the comments in the code.

% used PGFPlots v1.14
    \begin{filecontents}{data.txt}
        A B C D
        0 13 9 19
        1 0 1 5.5
        2 0 4 4
        3 1 3 14.5
        4 3 8 6
        5 1 8 6.5
        6 2 5 5.5
        7 0 7 14
        8 8 14 6
        9 0 5 12.5
        10 0 14 17.5
    \end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            width=\textwidth,
            % set space between adjacent bars to zero
            ybar=0pt,
            % adjust bar width so the bars are not overlapping with the bars
            % of another x value
            % (this depends on the chosen plot `width', `xmin' and `xmax' values
            bar width=7pt,
            % to show each x value
            xtick distance=1,
            % set `xmin' and `xmax' values manually so the bars aren't clipped
            xmin=-0.5,
            xmax=10.5,
            % set the tick length of the `xticks' to zero ...
            xtick style={
                % (I this key has to be prefixed by `/pgfplots`, because
                %  normally here are just expected tikz keys)
                /pgfplots/major tick length=0pt,
            },
            % but show them *between* the x values together with the grid
            extra x ticks={-0.5,0.5,...,10.5},
            extra x tick labels=\empty,
            extra x tick style={
                grid=major,
                % reset the tick length to the default value
                % (which otherwise would be the same as for the normal ticks
                %  which is set to zero in this case --> see above)
                xtick style={
                    /pgfplots/major tick length=4pt,
                },
            },
        ]
            \addplot table [x=A,y=B] {data.txt};
            \addplot table [x=A,y=C] {data.txt};
            \addplot table [x=A,y=D] {data.txt};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

  • Nice! But I was hoping a solution which doesn't involve manual intervention. – Denis Bitouzé Dec 11 '16 at 17:20
  • 1
    When you mean with "manual intervention" that bar width is automatically calculated depending on ... (this will be a long list) then I guess that there (currently, i.e. with PGFPlots v1.14) is no such feature. – Stefan Pinnow Dec 11 '16 at 17:24

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.