# Put labels inside stacked bar and customize legend

I have to create a horizontal stacked bar. But I need to put the percentage inside the bar, and the customized legend beside the figure.

This is my code in latex:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\pgfplotsset{width=10cm,compat=1.8}

\begin{tikzpicture}
\begin{axis}[
xbar stacked,
bar width = 25pt,
xmin = -1,
xmax = 101,
symbolic y coords={C, C++, Java, JavaScript, PHP, Python, Ruby},
x tick label style={font=\small},
xticklabels = {,,},
]
\addplot+[xbar, blue!50!green, thick, fill=white] plot coordinates
{(91,C) (87,C++) (72,Java) (91,JavaScript) (93,PHP) (91,Python) (87,Ruby)};
\addplot+[xbar, red, thick, fill=white] plot coordinates
{(9,C) (13,C++) (28,Java) (9,JavaScript) (7,PHP) (9,Python) (13,Ruby)};
\end{axis}
\end{tikzpicture}

\end{document}


The code only shows the stacked bar without the labels inside the bar and I need to add the legend beside the graph. This is the graph I would like to see:

Any help would be appreciated.

I had a crack at it and worked out a way. I used nodes near coords for the percentages within the bars. Furthermore, I used \addlegendimage and \addlegendentry for the legend beside the chart. Also I changed compat= to 1.9.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\pgfplotsset{width=10cm,compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
bar width = 25pt,
xmin = -1,
xmax = 101,
symbolic y coords={C, C++, Java, JavaScript, PHP, Python, Ruby},
x tick label style={font=\small},
xticklabels = {,,},
legend style={at={(1.05,0.6)},anchor=north west, draw=none},
nodes near coords,
every node near coord/.append style={font=\footnotesize},
point meta = explicit symbolic
]
\addplot+[xbar, blue!50!green, thick, fill=white] plot coordinates
{(91,C)[91]
(87,C++)[87]
(72,Java)[72]
(91,JavaScript)[91]
(93,PHP)[93]
(91,Python)[91]
(87,Ruby)[87]};
\addplot+[xbar, red, thick, fill=white] plot coordinates
{(9,C)[9]
(13,C++)[13]
(28,Java)[28]
(9,JavaScript)[9]
(7,PHP)[7]
(9,Python)[9]
(13,Ruby)[13]};


• Welcome to TeX.SX! A tip: You can use backticks ` to mark your inline code as I did in my edit. Jan 18 '20 at 12:54