# Drawing histogram: color depending on percentage

I would love to color each bar according to its percentage. Is there a way to do this? Or give it like a very sharp color/style. Any ideas are welcomed!!!

This is what my actual code yields:

\documentclass[11pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
xlabel near ticks,
ylabel near ticks
}

\begin{document}
\begin{tikzpicture}[font=\small]
\begin{axis}[
ybar,
bar width=20pt,
xlabel={Rating},
ylabel={Percentage},
ymin=0,
ytick=\empty,
xtick=data,
axis x line=bottom,
axis y line=left,
enlarge x limits=0.2,
xticklabel style={anchor=base,yshift=-\baselineskip},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
]
(1, 6.110)
(2, 11.370)
(3 ,    27.145)
(4  ,34.174)
(5  ,21.201)
};
\end{axis}
\end{tikzpicture}
\end{document}


One may use marks to fill the bars. To this end a new mark ybar is declared, which is just a rectangle. These rectangles are very tall, but get cut at the axis with clip marker paths=true. (You might add axis on top if you want the x axis line on top.) Using fill=mapped color will then fill the bars with a color that derives from the point meta.

\documentclass[11pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
xlabel near ticks,
ylabel near ticks
}

\pgfkeys{/pgf/shapes/ybar/height/.initial=10cm,/pgf/shapes/ybar/width/.initial=20pt}
\pgfdeclareplotmark{ybar}{%
\pgfpathrectangle{\pgfpoint{-.5*\pgfkeysvalueof{/pgf/shapes/ybar/width}}{0pt}}{%
\pgfpoint{\pgfkeysvalueof{/pgf/shapes/ybar/width}}{-\pgfkeysvalueof{/pgf/shapes/ybar/height}}}
\pgfusepath{stroke,fill}}

\begin{document}
\begin{tikzpicture}[font=\small]
\begin{axis}[ybar,
bar width=20pt,
/pgf/shapes/ybar/width=\pgfplotbarwidth, %<- synchronizes the width
xlabel={Rating},
ylabel={Percentage},
ymin=0,ymax=40,
ytick=\empty,
xtick=data,
axis x line=bottom,
axis y line=left,
enlarge x limits=0.2,
xticklabel style={anchor=base,yshift=-\baselineskip},
clip marker paths=true, %< cut away unwanted parts
scatter/use mapped color={draw=black,fill=mapped color}, % <- fill marks according to meta
nodes near coords*={\pgfmathprintnumber\pgfplotspointmeta\%},
]
(1, 6.110)
(2, 11.370)
(3 ,    27.145)
(4  ,34.174)
(5  ,21.201)
};
\end{axis}
\end{tikzpicture}
\end{document}


Of course, one could do the same for xbar. I add this here because it may be useful for others.

\documentclass[11pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
xlabel near ticks,
ylabel near ticks
}

\pgfkeys{/pgf/shapes/xbar/height/.initial=10cm,/pgf/shapes/xbar/width/.initial=20pt}
\pgfdeclareplotmark{xbar}{%
\pgfpathrectangle{\pgfpoint{0pt}{-.5*\pgfkeysvalueof{/pgf/shapes/xbar/width}}{0pt}}{%
\pgfpoint{-\pgfkeysvalueof{/pgf/shapes/xbar/height}}{\pgfkeysvalueof{/pgf/shapes/xbar/width}}}
\pgfusepath{stroke,fill}}

\begin{document}
\begin{tikzpicture}[font=\small]
\begin{axis}[xbar,
bar width=20pt,
/pgf/shapes/xbar/width=\pgfplotbarwidth,
ylabel={Rating},
xlabel={Percentage},
xmin=0,xmax=42, %<- from the hitchhikers guide
xtick=\empty,
ytick=data,
axis x line=bottom,
axis y line=left,
enlarge y limits=0.2,
yticklabel style={anchor=base,xshift=-\baselineskip},
point meta=x,
clip marker paths=true, %< cut away unwanted parts
scatter/use mapped color={draw=black,fill=mapped color}, % <- fill marks according to meta
nodes near coords*={\pgfmathprintnumber\pgfplotspointmeta\%},
nodes near coords style={anchor=west}
]
coordinates {
(6.110,1)
(11.370,2)
(27.145,3)
(34.174,4)
(21.201,5)
};
\end{axis}
\end{tikzpicture}
\end{document}


• As always.... Chapeau!
– user189304
May 31 '19 at 14:56
• The last version looks awesome! It would be even prettier if you’d put the percentages inside the bars!
– user189304
May 31 '19 at 22:30
• @Delan nodes near coords style={anchor=west} -> nodes near coords style={anchor=east} but then xmax=42, %<- from the hitchhikers guide is no longer optimal. ;-)
– user121799
May 31 '19 at 22:33