2

enter image description here

Problems:

  • The bar value "90" is written over a black line, almost impossible to read
  • The value "58" is written over an x tick, hard to ready
  • Bar spacing is bad. There is too much space between the bars, and there is no space above/below the top and the bottom bar.

Some of this might be my fault, but I would expect that this kind of mess should be automatically avoided by pgfplots. What am I doing wrong?

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}

\begin{document}

\begin{figure}
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\begin{axis}[
        xbar,
            xmin=0.0,
            xmax=91.6666666667,
            width=12cm,
            height={ 1cm + ( 3.0 * 1cm ) },
    symbolic y coords={{Térritmus},{Mozgásritmus},{Formaritmus}},
    xlabel={Pontérték \%},
    ytick=data,
    nodes near coords,
    nodes near coords align = {horizontal}
]
\addplot [draw=black, fill=cyan!40!black] coordinates {
    (58.3333333333,{Térritmus})
    (91.6666666667,{Mozgásritmus})
    (90.0,{Formaritmus})

};
\end{axis}
\end{tikzpicture}
}
\caption{Ritmus}
\end{figure}

\end{document}
1

2 Answers 2

4

The first problem is caused by xmax=91.6666666667. Since you have set it, pgfplots does not try to calculate the upper x axis limit. Moreover, nodes near every coordinate are not taken into account while calculating the limits.

To solve the first problem, remove xmax=91.6666666667 and add enlarge x limits={rel=0.13,upper} (the value 0.13 have to be selected arbitrarily). To solve the second and third problem, add enlarge y limits=0.4 (again, 0.4 is arbitrary).

The another issue is that my LaTeX with pgfplots 1.8 does not want to compile the code with diacritics in symbolic y coords. You can use ytick and yticklabels to be 100% reproducible.

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xbar,
    xmin=0.0,
    width=12cm,
    height=4cm,
    enlarge x limits={rel=0.13,upper},
    ytick={1,2,3},
    yticklabels={{Térritmus},{Mozgásritmus},{Formaritmus}},
    enlarge y limits=0.4,
    xlabel={Pontérték \%},
    ytick=data,
    nodes near coords,
    nodes near coords align=horizontal
]
\addplot [draw=black, fill=cyan!40!black] coordinates {
    (58.3333333333,1)
    (91.6666666667,2)
    (90.0,3)

};
\end{axis}
\end{tikzpicture}

\end{document}

compiled code

To solve the first problem you can also move the nodes inside the bars. But you should increase the bar width (e.g., to \baselineskip):

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xbar,
    bar width=\baselineskip,
    xmin=0.0,
    width=12cm,
    height=4cm,
    ytick={1,2,3},
    yticklabels={{Térritmus},{Mozgásritmus},{Formaritmus}},
    enlarge y limits=0.4,
    xlabel={Pontérték \%},
    ytick=data,
    nodes near coords,
    nodes near coords align=left,
    every node near coord/.style={color=white}
]
\addplot [draw=black, fill=cyan!40!black] coordinates {
    (58.3333333333,1)
    (91.6666666667,2)
    (90.0,3)

};
\end{axis}
\end{tikzpicture}

\end{document}

compiled code

1
  • Although I noticed that below 10% percent value, moving nodes into bars doesn't look good either. Goes out of the bar on the left side. So in some cases, enlarge x limits is better.
    – nagylzs
    Commented Apr 18, 2013 at 22:18
2

There are a few things you can try here: perhaps

every node near coord/.append style={anchor=east},

which gives

screenshot

or else changing xmax to something else, which gives (for example)

enter image description here

Furthermore, you can change the height and bar width to get round the overlapping with the xtick

  height={ .5\textwidth},
  bar width= .8cm,

screenshot

In either case, I would remove resizebox and use width=\textwidth; using resizebox may lead to inconsistent font sizes in your diagram, which would negate part of the point of using pgfplots in the first place.

% arara: pdflatex
% !arara: indent: {overwrite: on, trace: yes}
\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}


\begin{tikzpicture}
    \begin{axis}[
            xbar,
            xmin=0.0,
            xmax=110,
            height={ .5\textwidth},
            bar width= .8cm,
            symbolic y coords={{Térritmus},{Mozgásritmus},{Formaritmus}},
            xlabel={Pontérték \%},
            ytick=data,
            nodes near coords,
            nodes near coords align = {horizontal},
            %every node near coord/.append style={anchor=east},
            width=\textwidth,
        ]
        \addplot [draw=black, fill=cyan!40!black] coordinates {
            (58.3333333333,{Térritmus})
            (91.6666666667,{Mozgásritmus})
            (90.0,{Formaritmus})

        };
    \end{axis}
\end{tikzpicture}


\end{document}
1
  • For some reason, using width=\textwidth is wrong. The whole figure goes out of margins. Example This is why I started to use a fixed size. I'm not sure how to overcome that problem.
    – nagylzs
    Commented Apr 19, 2013 at 5:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .