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 the following xbar plot. I have two problems:

1) to see all the bars I have to do "enlarge y limits = X", where X is found by trial and error. This seems to result in the bars not being positioned correctly in relation to the values on the y axis. What is going on?

2) I want to label the bars and have set "nodes nears coords align=horizontal" as suggested in the manual and in this question but it doesn't seem to work. Any ideas why?

%% LyX 2.0.3 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{pgfplots}

\makeatother

\usepackage{babel}
\begin{document}
\begin{tikzpicture}[]
\begin{axis}[xbar,
width=10cm,
height=8cm,
enlarge y limits=0.5,
bar width=7pt,
nodes near coords,
nodes near coords align=horizontal,
point meta=explicit symbolic,]
\addplot+ coordinates {(1,1) [label1]};
\addplot+ coordinates {(3,2) [label2]};
\addplot+ coordinates {(5,3) [label3]};
\addplot+ coordinates {(6,4) [label4]};
\addplot+ coordinates {(4,5) [label5]};
\addplot+ coordinates {(16,6) [label6]};
\addplot+ coordinates {(6,7) [label7]};

\end{axis}
\end{tikzpicture}
\end{document}
share|improve this question

1 Answer 1

up vote 3 down vote accepted
  1. If you supply xbar to the axis, each \addplot command will have a different vertical offset. This allows you to have two bars with the same nominal y coordinate that don't overlap. For example if you have

    \addplot coordinates {(1,1) (3,2) (1,3)};
    \addplot coordinates {(1,1) (2,2) (3,3)};
    

    you'll get

    Note that none of the bars are actually at the specified vertical positions (1,2,3), but they're either above or below. This is a really useful feature in most cases. In your application, however, each series (i.e. each \addplot command) only has one data point, so the offset is undesirable, especially since the axis limits aren't automatically adjusted. To switch off the offset, just add bar shift=0pt to your axis options and remove the enlarge y limits:

  2. nodes near coordinates align only works with numerical metadata, because it uses the sign of the value to decide whether to put the label on the left or the right of the bar. To place symbolic labels to the right of the bars, use every node near coord/.append style={anchor=west}. Note that the axis limits won't automatically be adjusted to accommodate the labels, so you might have to set something like enlarge x limits={upper, value=0.2}.

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{pgfplots}

\makeatother

\usepackage{babel}
\begin{document}
\begin{tikzpicture}[]
\begin{axis}[xbar,
xmin=0,
enlarge x limits={upper, value=0.2},
width=10cm,
height=8cm,
bar shift=0pt,
bar width=7pt,
nodes near coords,
every node near coord/.append style={anchor=west},
point meta=explicit symbolic,]
\addplot+ coordinates {(1,1) [label1]};
\addplot+ coordinates {(3,2) [label2]};
\addplot+ coordinates {(5,3) [label3]};
\addplot+ coordinates {(6,4) [label4]};
\addplot+ coordinates {(4,5) [label5]};
\addplot+ coordinates {(16,6) [label6]};
\addplot+ coordinates {(6,7) [label7]};

\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
1  
Perfect, just what I needed. pgfplots is so logical once you know it... –  user8786 Jun 6 '12 at 13:10

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.