# pgfplots: auto-size chart?

I have an xbar graph with some some data that I don't necessarily know the length of beforehand. Is there some way I can tell pgfplots to automatically expand the height of the chart according to the amount of data?

For example, in this example, there are so many data points that the bars overlap because the height is constant.

\documentclass[letterpaper]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{filecontents}{my.dat}
Label     value       num
992       70          1
993       120         2
994       30          3
995       330         4
999       50          5
988       50          6
989       50          7
983       50          8
973       50          9
963       50          10
953       50          11
941       60          12
942       70          13
943       20          14
944       30          15
945       10          16
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
xbar,
xlabel=My value,
ylabel=Label here,
xmajorgrids=true,
ytick=data,
yticklabels from table={my.dat}{Label},
nodes near coords,
]
\addplot table [x=value, y=num]
{my.dat};
\end{axis}

\end{tikzpicture}

\end{document}


-

If you set the length of the y vector to a dimension with a unit, such as y=0.5cm, the chart will automatically stretch or shrink according to the number of entries.

To make all the bars fit into the chart area, you can set enlarge y limits={true, abs value=0.75}, which will add 0.75 units at the top and bottom of the chart, and to make the data labels fit, you can set enlarge x limits={upper, value=0.15}, which will add 15% extra space on the right edge of the chart.

Note that with bar and column charts, you should usually also set the minimum value, in this case xmin=0 to make sure the complete bars are visible.

And finally, pgfplots automatically loads tikz, so you don't need to load that package manually.

\documentclass[letterpaper]{article}
\usepackage{pgfplots}
\begin{filecontents}{my.dat}
Label     value       num
992       70          1
993       120         2
994       30          3
995       330         4
999       50          5
988       50          6
989       50          7
983       50          8
973       50          9
963       50          10
953       50          11
941       60          12
942       70          13
943       20          14
944       30          15
945       10          16
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
xbar,
y=0.5cm, enlarge y limits={true, abs value=0.75},
xmin=0, enlarge x limits={upper, value=0.15},
xlabel=My value,
ylabel=Label here,
xmajorgrids=true,
ytick=data,
yticklabels from table={my.dat}{Label},
nodes near coords, nodes near coords align=horizontal
]
\addplot table [x=value, y=num]
{my.dat};
\end{axis}

\end{tikzpicture}

\end{document}

-
This works great, and thanks for fixing the other errors too! –  erjiang Oct 16 '11 at 3:48