# Centering bars on xticks in pgfplots

My wife is working on her master thesis and we are trying to make some bar graphs to use with it. We have managed to create a nice-looking graph, but the labels/xtick does not align in center. When adding more addplots, things get even worse. We're both new to LaTex, and even after quite a few hours of browsing websites and manuals, I cannot find a solution to this problem.

My first try on a "MWE":

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{tipa}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{width=10 cm}
\begin{axis} [
symbolic x coords={Label 1,Label 2},
xtick={Label 1, Label 2},
x tick label style={rotate=45, anchor=east, align=center},
axis lines=left,
y label style={rotate=-90},
ylabel=\Large{\textbf{\%}},
legend style={at={(0.5,-0.10)},
anchor=north,legend columns=1},
ybar=0pt ,
ymin=0,
ymax=50,
samples=2,
domain=1:2,
bar width=2.5cm,
enlarge x limits={abs=3.2cm},
]
coordinates{ (Label 1,35.5) } ;
coordinates{ (Label 2,14.8) } ;

\end{axis}
\end{tikzpicture}

\end{document}


Thank you!

-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  karlkoeller Mar 6 '14 at 8:50

Is it like this?

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{tipa}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{width=10 cm}
\begin{axis} [
symbolic x coords={Label 1,Label 2},
xtick={Label 1, Label 2},
x tick label style={rotate=45, anchor=east, align=center},
axis lines=left,
y label style={rotate=-90},
ylabel=\Large{\textbf{\%}},
legend style={at={(0.5,-0.10)},
anchor=north,legend columns=1},
ybar=0pt ,
ymin=0,
ymax=50,
samples=2,
domain=1:2,
bar width=0.5cm,                       %% changed
ybar=-0.5cm,                           %% new
enlarge x limits={abs=3.2cm},
nodes near coords,                     %% new
nodes near coords align={vertical},    %% new
]
coordinates{ (Label 1,35.5) } ;
coordinates{ (Label 2,14.8) } ;

\end{axis}
\end{tikzpicture}

\end{document}


I have marked the changes made in the code. I have changed the bar width to 0.5cm (assuming that you may need it when more bars are added) and shifted them by the suitable amount so that x tick and labels align in the center by

bar width=0.5cm,                       %% changed
ybar=-0.5cm,                           %% new


I have also added the y values of the bars at the top by

nodes near coords,                     %% new
nodes near coords align={vertical},    %% new


If you want labels also to be centered, you can use xshift and yshifts in

x tick label style={rotate=45, anchor=east,
align=center,xshift=0.4cm,yshift=-0.4cm},


Change the shift values appropriately.

Hope that is useful.

-