1

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: enter image description here

Any help would be appreciated.

2

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     
        ]
    \addlegendimage{empty legend},    
    \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]};

    \addlegendentry{\hspace{-.6cm}\textbf\small{Type}},
    \addlegendentry{Seed},
    \addlegendentry{No seed},
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here I am pretty new to this, so my coding can come across a bit mundane. I hope this helps. All the best!

3
  • 1
    Welcome to TeX.SX! A tip: You can use backticks ` to mark your inline code as I did in my edit.
    – dexteritas
    Jan 18 '20 at 12:54
  • 1
    It might be good if you could add an image of the result, so everyone can see it directly.
    – dexteritas
    Jan 18 '20 at 12:55
  • 1
    Thanks for the suggestion and editing. I am learning a lot here.
    – Emmo03
    Jan 18 '20 at 13:01

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.