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.

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{\%}},                                 
title= \Large{Some Heading} ,               
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},   
]
\addplot [blue,fill=blue]                       
coordinates{ (Label 1,35.5) } ;
\addplot [red,fill=red]
coordinates{ (Label 2,14.8) } ;

\end{axis}
\end{tikzpicture}

\end{document}

Please help us find a solution. Suggestions of other (simpler/better) packages to use are also welcome.

Thank you!

share|improve this question
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  karlkoeller Mar 6 at 8:50

1 Answer 1

Is it like this?

enter image description here

\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{\%}},
title= \Large{Some Heading} ,
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
]
\addplot [blue,fill=blue]
coordinates{ (Label 1,35.5) } ;
\addplot [red,fill=red]
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.

share|improve this answer

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.