0

I'm a little bit confused why latex messes an easy drawing up. More precisely, I use the following code:


\begin{tikzpicture}
\begin{axis}[title  = {Tex example},
xbar,
ytick             = {Part1, Part2, Part3, Part4, Part5, Part6, Part7},
symbolic y coords = {Part1, Part2, Part3, Part4, Part5, Part6, Part7},
nodes near coords,
]
\addplot coordinates {(40.2,Part1)};
\addplot coordinates {(30.9,Part2)};
\addplot coordinates {(10.2,Part3)};
\addplot coordinates {(10.1,Part4)};
\addplot coordinates {(4.3,Part5)};
\addplot coordinates {(2.5,Part6)};
\addplot coordinates {(1.0,Part7)};
\end{axis}
\end{tikzpicture}

But this generates the following picture: How do I get the y labels aligned to the according bars? (And may reduce the distance between the bars in general)? enter image description here

1

1 Answer 1

0

LaTeX or in the case PGFPlots, does not mess anything up. The normal behaviour is to shift the bars, so that multiple plots does not lie on top of each other. Your data does not really need multiple \addplot, but if the bars are really considered separate plot that can have separate legend entries, you can do it like this:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={Tex example},
xbar,
symbolic y coords={Part1, Part2, Part3, Part4, Part5, Part6, Part7},
ytick distance=1,
nodes near coords,
bar shift=0pt,
bar width=18pt,
enlarge x limits={value=0.2, upper},
]
\addplot coordinates {(40.2,Part1)};
\addplot coordinates {(30.9,Part2)};
\addplot coordinates {(10.2,Part3)};
\addplot coordinates {(10.1,Part4)};
\addplot coordinates {(4.3,Part5)};
\addplot coordinates {(2.5,Part6)};
\addplot coordinates {(1.0,Part7)};
\end{axis}
\end{tikzpicture}
\end{document}

ybar plot

A more normal way(dependent on what your data really represent) would be to use:

\addplot coordinates {(40.2,Part1) (30.9,Part2) (10.2,Part3) (10.1,Part4) (4.3,Part5) (2.5,Part6) (1.0,Part7)};
2
  • Thank you for the answer fixing all design problems at all! I used multiple plots to get different colors for the bars. But it's still kinda annoying that pgfplots does not automatically expand the diagram as needed for all the plots.
    – Kevin
    Sep 16 at 17:38
  • There is nothing annoying and no messing up. You must acknowledge that your use case is not the only one and that PGFPlots has no way of knowing. PGFPlots behaves by default the way that suits most people. Please remember to accept the answer. Sep 16 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.