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}
Best Answer
If you supply
xbar
to theaxis
, each\addplot
command will have a different vertical offset. This allows you to have two bars with the same nominaly
coordinate that don't overlap. For example if you haveyou'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 addbar shift=0pt
to your axis options and remove theenlarge y limits
: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, useevery 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 likeenlarge x limits={upper, value=0.2}
.