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 am using LaTeX, TikZ and PGFplot in order to automatically generate several series of diagrams, one of which is comprised of bar charts. Each bar in each bar chart may have a unique label and height -- I have used PGFplot before and I know that I can read in coordinates from files, but would it be possible for me to create labels for each bar using textual data stored in a tab-separated file? If so, how would I do this? A simplified example of a file I might use is shown below.

# foo.dat
# Label | X-Position | Height

A    1    15
B    2    20
C    3    12
D    4    24

Thanks for your help!

share|improve this question
add comment

1 Answer 1

up vote 13 down vote accepted

Depending on whether you want to use the label in place of the x tick label, or as additional information to each bar, here are two approaches.

You can use the key xticklabels from table={<table or file name>}{<column name>} to specify which column to read the labels from. These will be displayed instead of the x-coordinates of the bars. Note that you should also use the option xtick=data (keyword "data" is an in-built) to make sure that ticks are only created in those positions where a bar is drawn.

If you want to keep the x tick labels, you can add the textual labels at the top of each bar by using nodes near coords. This will add a text node above each bar, which will contain the meta data for that point. By default, this is the y coordinate. You can specify which column to use for the meta data by using meta=<column name> as an option to the table keyword. Note that for text data, you have to provide the option point meta=explicit to the plot, otherwise numerical data is expected.

PGFplots data labels from file

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{testdata.dat}
Label X-Position Height
A    1    15
B    2    20
C    3    12
D    4    24
\end{filecontents}
%
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    xtick=data,% crucial line for the xticklabels directive
    ymin=0,
    xticklabels from table={testdata.dat}{Label}
]

\addplot table [
    x=X-Position,
    y=Height
] {testdata.dat};

\end{axis}
\end{tikzpicture}
%
\hspace*{0.5cm}
%
\begin{tikzpicture}
\begin{axis}[
    ybar,
    xtick=data,
    ymin=0
]

\addplot +[
    nodes near coords,
    point meta=explicit symbolic
]
    table [x=X-Position,
        y=Height,
        meta=Label
] {testdata.dat};

\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Exactly what I wanted -- thank you very much! –  void-pointer Jun 21 '11 at 6:25
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.