# Bar chart - Single colored bar

For the bar chart proposed, I would like to use different color for each single bar. How can I do this?

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
title=Title,
xbar,
xmajorgrids = true,
bar width=6mm,
width=12cm, height=5.5cm,
enlarge y limits=0.2,
xlabel={\#number},
symbolic y coords={A,B,C,D},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
]

\addplot coordinates {(1,A) (7,B) (5,C)(2,D)};
\end{axis}
\end{tikzpicture}

\end{document}

• possible duplicate of colors and legend in groupplots – Jake Aug 13 '13 at 14:23
• To the person who downvoted this: Please always leave a comment explaining why you think the question needs improvement. In general, downvotes on this site are very rare compared to other sites on the Stackexchange network: Downvotes are usually reserved for spam or malicious posts. This question is neither. – Jake Aug 13 '13 at 14:29
• HI Jake, the question I proposed is not the same you indicated. For a single chart I would like to color each single bar differently. The example you indicated explain three different chart and all bar of a single chart use the same color – user21431 Aug 13 '13 at 14:36

## 1 Answer

From my answer to Colors and legend in groupplots/barplot:

In order to colour each bar differently (...) each bar needs to be handled as its own plot, i.e. it needs its own \addplot ... command. Fortunately, you don't have to write \addplot ... four times, but instead you can use \pgfplotsinvokeforeach {0,...,3} { \addplot ... }.

I would suggest you provide the data in a table, either in an external datafile or in a macro created using \pgfplotstableread. This makes it easier to loop over the data, and it makes your data more maintainable (the coordinate syntax becomes quite tedious for larger datasets).

Also, instead of using symbolic coordinates, I usually find it easier to plot the values using \coordindex and create the labels using yticklabels from table.

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}

\begin{document}

\pgfplotsset{
select row/.style={
x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
}
}

\pgfplotstableread[header=false]{
1 A
7 B
5 C
2 D
}\datatable

\begin{tikzpicture}
\begin{axis}[
title=Title,
xbar, bar shift=0pt,
enlarge y limits=0.2,
xmin=0,
ytick={0,...,4},
yticklabels from table={\datatable}{1},
xmajorgrids = true,
bar width=6mm,
width=12cm, height=5.5cm,
xlabel={\#number},
nodes near coords, nodes near coords align={horizontal},
]

\pgfplotsinvokeforeach{0,...,4}{
\addplot table [select row=#1, y expr=#1] {\datatable};
}
\end{axis}
\end{tikzpicture}

\end{document}

• Your code doesn't work with beamer, does it? If I paste the tikzpicture between \begin{frame} and \end{frame}. Error: "Illegal parameter number in definition of \test". – GeMir Nov 10 '15 at 14:54
• @GeMir: It should work with beamer. Could you open a new question with a minimal example document that reproduces the issue? – Jake Nov 10 '15 at 15:43
• That's a pity you didn't keep it simple so anyone can copy and paste in beamer. I don't see any reason to use separate dataset for such simple example. I am getting the same error as GeMir. – nosbor Aug 31 '18 at 19:23