0

I'd like to make a bar chart with two basic conditions:

  • Each bar has a name (not a numeric value)
  • Each bar has its own color

I tried the following, but the result is terribly off. What am I missing?

\documentclass[a4paper, 12pt]{article}
\usepackage[paperwidth=5in, paperheight=3.2in]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\geometry{left=0mm, right=3mm,top=6mm, bottom=3mm,}

\begin{document} \begin{tikzpicture} \begin{axis}[ xbar, xmin=0, symbolic y coords = {cat1, cat2, cat3}]

    \addplot [fill=red] coordinates {(1,cat1)};
    \addplot [fill=green] coordinates {(2,cat2)};
    \addplot [fill=blue] coordinates {(3,cat3)};
\end{axis}
\end{tikzpicture}

\end{document}

Stefan Pinnow
  • 29,535
  • 2
    To avoid the "shifting" of the bars add bar shift=0pt and to avoid the "repeating" axis labels add ytick distance=1 to the axis options. – Stefan Pinnow Jan 30 '21 at 08:43
  • May I ask why you want to have each bar have its own color? In my opinion this is only useful when color would give another/additional information. That means, in your given example this is totally unnecessary. (But if you really want to/need to have the different colors, you have to use one \addplot command per color, as you already did in your example.) – Stefan Pinnow Jan 30 '21 at 08:45
  • Thank you for your answer, @StefanPinnow. Please post it as an answer and if it works I will accept it. The reason is that the scale shows criticality. Everywhere in my document, redder=more critical and greener=less critical. The actual graph I'm building should show how many items are rated on each criticality level. Color is indeed redundant with the y-labels, but it would look weird to have that one graph that breaks the color scheme. – guest_user Feb 01 '21 at 10:20
  • Update: I tried the solution above and it works. Thanks! – guest_user Feb 01 '21 at 12:56

0 Answers0