Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have only modified the pgfplots xbar example very little and it completely fails:

EDIT: Removed original example, it contained only a missing comma.

And do I really need to define for every entry a symbolic y coords? My complete list is about 40 entries.

EDIT: I implemented the answer with pgfplotstable, but the result looks wrong:

\pgfplotstableread[header=false, col sep=comma]{ % Read data table. 
% First row doesn't have column names, hence the "header=false"
0.34,LaTeX Kernel
0.36,pre document class
0.48,document class
0.54,template packages
0.64,encoding (documents)
0.72,encoding (files)
0.88,Fonts
0.98,Packages: Base
1.01,Packages: Bugfix
1.03,relsize
1.13,Math
1.43,Math (using LaTeX 3)
2.64,pgf/tikz
2.72,siunitx
2.81,Symbols
2.85,Tables
3.01,Text
3.04,Quotes (csquotes)
3.3475,Bibliography (biblatex)
3.365,Figures
3.48,Captions
3.58,Index
3.745,Glossary
3.85,{Verbatim, Listings}
3.91,Fancy
3.92,Layout
4.015,Head and Foot
4.03,Headings
4.33,PDF
4.46,Additional
4.55,Style.tex
6.57,Document
}\compilationtimes

\begin{tikzpicture}
  \begin{axis}[
    xbar, xmin=0,
    width=12cm, height=10cm, enlarge y limits=0.5,
    xlabel={compilation time / seconds},
    ytick=data,
    nodes near coords, nodes near coords align={horizontal},
    yticklabels from table={\compilationtimes}{1} % Get tables from second column of data table
  ]
  \addplot table [
            y expr=-\coordindex, % Use negative coordinate index as y coordinate
            x index=0 % Use first column as x coordinate
        ] {\compilationtimes};
  \end{axis}
\end{tikzpicture}

enter image description here

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

Peter's answer is spot-on to remove the error message. I'd just like to add an answer explaining how to get around the "defining all symbolic coordinates by hand" issue:

I would suggest not to use symbolic y coordinates, and don't use \addplot coordinates, both are pretty cumbersome. Instead, read your data into a data table using \pgfplotstableread. That allows you to re-use it more easily for different plots, and for a function called y tick labels from table={<table name>}{<column>}. That function reads entries from a data table (or file) to use as tick labels. If you use y expr=-\coordindex (or y expr=\coordindex if you want the first data point to be plotted at the bottom of the plot) together with ytick=data, the tick labels and data points will match up flawlessly, and if you choose to change the name for a row later on, you only have to do it in one place (in the data table).

To make sure all the bars fit into the plot without overlap, one approach is to let the plot area stretch vertically with the number of bars by setting the y unit vector using y=<length>, where <length> should be greater than the bar width. Also, enlarge y limits should be set to something like abs=0.5 to add half a unit length at the top and bottom of the plot. Setting enlarge y limits=0.5 (without the abs) increases the axis range by 50%, which is rarely what you want.

enter image description here

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


\pgfplotstableread[header=false, col sep=comma]{ % Read data table. 
% First row doesn't have column names, hence the "header=false"
0.34,LaTeX Kernel
0.36,pre document class
0.48,document class
0.54,template packages
0.64,encoding (documents)
0.72,encoding (files)
0.88,Fonts
0.98,Packages: Base
1.01,Packages: Bugfix
1.03,relsize
1.13,Math
1.43,Math (using LaTeX 3)
2.64,pgf/tikz
2.72,siunitx
2.81,Symbols
2.85,Tables
3.01,Text
3.04,Quotes (csquotes)
3.3475,Bibliography (biblatex)
3.365,Figures
3.48,Captions
3.58,Index
3.745,Glossary
3.85,{Verbatim, Listings}
3.91,Fancy
3.92,Layout
4.015,Head and Foot
4.03,Headings
4.33,PDF
4.46,Additional
4.55,Style.tex
6.57,Document
}\compilationtimes

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xbar, xmin=0,
    width=12cm,
    bar width=2ex, y=3ex, % Set the y unit vector, that way, the plot will stretch to accommodate all bars
    enlarge y limits={abs=0.75},
    xlabel={compilation time / seconds},
    ytick=data,
    nodes near coords, nodes near coords align={horizontal},
    yticklabels from table={\compilationtimes}{1} % Get tables from second column of data table
  ]
  \addplot table [
            y expr=-\coordindex, % Use negative coordinate index as y coordinate
            x index=0 % Use first column as x coordinate
        ] {\compilationtimes};
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
It works fine, however for many values the result does not look ok. –  Matthias Pospiech Feb 1 '13 at 22:33
    
@MatthiasPospiech: I've edited my answer to show how you can make it work with a lot of data points. –  Jake Feb 1 '13 at 22:39
add comment

You are missing a trailing , before ytick=data. After correcting that we obtain:

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xbar, xmin=0,
    width=12cm, height=3.5cm, enlarge y limits=0.5,
    xlabel={compilation time / seconds},
    symbolic y coords={LaTeX Kernel,pre document class},% <--- Missing comma here
    ytick=data,
    nodes near coords, nodes near coords align={horizontal},
    ]
    \addplot coordinates {
    (0.34,LaTeX Kernel)
    (0.36,pre document class)
    };
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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