1

I have the following graph and I have no idea why it is not centering.

example

Here is the code as a minimum working example:

\documentclass[]{thesis}

\usepackage{graphicx}
\graphicspath{{figures/}} % folder


\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{dateplot}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}

\begin{figure}[h]
    \centering
    \begin{tikzpicture}
    \begin{axis}[
    xbar, 
    y = 1.2cm,
    y axis line style = { opacity = 0 }, 
    hide x axis, 
    tickwidth = 0pt,
    xmin=0, 
    xlabel={Percentage \%},
    enlarge y limits  = 0.2,
    enlarge x limits  = 0.02,
    bar width=15pt,
    symbolic y coords={%
        {Drunken driving},
        {Unadjusted speed},
        {Insufficient safety distance},
        {Not respecting right of way},
        {Turning, U-turn, reversing, entering the flow of traffic, starting off the edge of the road}},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
    nodes near coords align={horizontal},
    ytick=data,yticklabel style={text width=10cm,align=right,font=\linespread{1.2}\selectfont},
    ]
    \addplot[style={blue,fill=blue,mark=none}] coordinates {
        (3,{Drunken driving})
        (10.7,{Unadjusted speed})
        (15.9,{Insufficient safety distance}) 
        (17.3,{Not respecting right of way}) 
        (19,{Turning, U-turn, reversing, entering the flow of traffic, starting off the edge of the road})
    };
    \end{axis}
    \end{tikzpicture}
    \caption{Causes of accidents with personal injuries caused by human error in Germany in 2018.}
    \label{fig:accidents}
\end{figure}

\end{document}
sollniss
  • 155
  • 1
    Are you using a custom thesis documentclass, or the one available in MikTeX? – Torbjørn T. Oct 08 '19 at 16:53
  • @TorbjørnT. I'm using a custom one from my university. The thesis is just for the example. – sollniss Oct 08 '19 at 16:54
  • 1
    It would be better to use a standard class such as report or article then. – Torbjørn T. Oct 08 '19 at 16:55
  • 1
    As far as I can see my first comment was correct, the additional whitespace seems to come from the text width in the yticklabel style. Try adding draw to the yticklabel style as well, then you'll see the outline of those nodes. – Torbjørn T. Oct 08 '19 at 16:58
  • 2
    Try adding scale only axis,width=0.45\textwidth to the axis options, and use text width=0.45\textwidth in the yticklabel style. – Torbjørn T. Oct 08 '19 at 17:01
  • Works perfectly, thanks. – sollniss Oct 08 '19 at 17:53
  • Would you perhaps also know why the y axis text is not perfectly aligned to tbe bar's center? The text is slightly too high I believe. I can make a separate question if it's not trivial. – sollniss Oct 08 '19 at 17:56

1 Answers1

2

As I first suspected, it is caused by the text width setting the yticklabel style, which makes those nodes very wide. One way of avoiding guesswork for appropriate widths is to add scale only axis,width=0.45\textwidth to the axis options, and use text width=0.45\textwidth in the yticklabel style.

Regarding the small misalignment, it is because of the text height and text depth. A quick fix is to add \strut to the font key for the yticklabel style.

\documentclass{report}

% this is added just for example, to make textblock of similar width to that in screenshot
\usepackage[margin=3cm]{geometry} 

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

\begin{document}

\begin{figure}[ht] % having h as the only placement specifier can lead to https://tex.stackexchange.com/q/1511
    \centering
    \begin{tikzpicture}
    \begin{axis}[
    xbar, 
    y = 1.2cm,
    scale only axis,width=0.45\textwidth, % <-- added
    y axis line style = { opacity = 0 }, 
    hide x axis, 
    tickwidth = 0pt,
    xmin=0, 
    xlabel={Percentage \%},
    enlarge y limits  = 0.2,
    enlarge x limits  = 0.02,
    bar width=15pt,
    symbolic y coords={%
        {Drunken driving},
        {Unadjusted speed},
        {Insufficient safety distance},
        {Not respecting right of way},
        {Turning, U-turn, reversing, entering the flow of traffic, starting off the edge of the road}},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
    nodes near coords align={horizontal},
    ytick=data,
    yticklabel style={
%      draw, % can be  useful for debugging
      text width=0.45\textwidth, % <-- added
      align=right,
      font=\linespread{1.2}\selectfont\strut % <-- added \strut
    }
    ]
    \addplot[style={blue,fill=blue,mark=none}] coordinates {
        (3,{Drunken driving})
        (10.7,{Unadjusted speed})
        (15.9,{Insufficient safety distance}) 
        (17.3,{Not respecting right of way}) 
        (19,{Turning, U-turn, reversing, entering the flow of traffic, starting off the edge of the road})
    };
    \end{axis}
% shows bounding box of tikzpicture, can also be useful for debugging
% \draw (current bounding box.south east) rectangle (current bounding box.north west);
    \end{tikzpicture}
    \caption{Causes of accidents with personal injuries caused by human error in Germany in 2018.}
    \label{fig:accidents}
\end{figure}

\end{document}

enter image description here

Torbjørn T.
  • 206,688