8

I am plotting a table with vertical bars and I get in trouble for large tables since the vertical bars are overlapping. Is there an option to automatically adjust the bar width?

enter image description here

Here is the code producing overlapping :

\documentclass{standalone}
\usepackage{filecontents,pgfplots}

\begin{filecontents}{pistonkinetics.dat}
x y
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9
10  10
11  11
12  12
13  13
14  14
15  15
16  16
17  17
18  18
19  19
20  20
21  21
22  22
23  23
24  24
25  25
26  26
27  27
28  28
29  29
30  30
31  31
32  32
33  33
34  34
35  35
36  36
37  37
38  38
39  39
40  40
\end{filecontents}

\begin{document}
\begin{tikzpicture}{width=10cm,height=5cm}
\begin{axis}
\addplot+[ybar,mark=none] plot table{pistonkinetics.dat};
\end{axis}
\end{tikzpicture}

\end{document} black,fill=red
6

If you're using a recent version of PGFPlots (at least 1.8) and your bars are equal distances apart, you can call \pgfplotsset{compat=1.8} before your axis and then set bar width=1 to make each bar one axis unit wide.

If you're using an older version of PGFPlots, or your bars aren't all the same width but you still want to avoid any gaps, you can use ybar interval. Note that for this you need to add a dummy entry at the end of your datafile, otherwise the last bar will be missing (each bar in ybar interval needs a start and end value):


Code for first example:

\documentclass[border=5mm]{standalone}
\usepackage{filecontents,pgfplots}

\begin{filecontents}{pistonkinetics.dat}
x y
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9
10  10
11  11
12  12
13  13
14  14
15  15
16  16
17  17
18  18
19  19
20  20
21  21
22  22
23  23
24  24
25  25
26  26
27  27
28  28
29  29
30  30
31  31
32  32
33  33
34  34
35  35
36  36
37  37
38  38
39  39
40  40
\end{filecontents}

\begin{document}
\begin{tikzpicture}{width=10cm,height=5cm}
\pgfplotsset{compat=1.8}
\begin{axis}
\addplot+[ybar,mark=none, bar width=1] plot table{pistonkinetics.dat};
\end{axis}
\end{tikzpicture}

\end{document}

Code for second example:

\documentclass[border=5mm]{standalone}
\usepackage{filecontents,pgfplots}

\begin{filecontents}{pistonkinetics.dat}
x y
1   1
2   2
5   5
8   8
9   9
10  10
11  11
13  13
15  15
17  17
18  18
19  19
23  23
24  24
27  27
28  28
32  32
33  33
34  34
35  35
36  36
37  37
39  39
40  40
41  0
\end{filecontents}

\begin{document}
\begin{tikzpicture}{width=10cm,height=5cm}
\pgfplotsset{compat=1.8}
\begin{axis}
\addplot+[ybar interval,mark=none, bar width=1] plot table{pistonkinetics.dat};
\end{axis}
\end{tikzpicture}

\end{document}

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.