I would like to create a bar chart with a multicolored bar. The bar chart should show the length of each section. My problem is that in the first section the number always goes before the bar chart and not on it.

My code is this one:

\begin{axis}[ \
    ybar stacked,\
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},\
    point meta=rawy,\
    axis lines=none,\
    bar width=1cm,\
    x=7cm, \
    nodes near coords style={rotate=90, anchor=west}\
\addplot coordinates{(0,148) }; \
\addplot coordinates{(0,1)  }; \
\addplot coordinates{(0,361)  }; \

This is how it looks like

But i want that it looks like this:

enter image description here

  • Welcome. // Please post code, which compiles. E.g. the trainling \ mess up, the preamble is guessworking etc. Thank you
    – MS-SPO
    Feb 28 at 14:59

2 Answers 2


Here's a way to do it. Because you didn't provide a preamble, here's a way to do it in Tikz:

  • perceive bars as lines, which need to be a bit thicker
  • calculate the required lengths manually, as needed (or use the calc-lib)
  • put nodes (without \) before the path finishes at ;
  • provide and finetune useful styles

Kindly look up these commands in the pgfmanual.



    lw/.style={line width=12mm},
    rb/.style={lw,red },
    \draw[rb] (0,0)             -- node[wh]{148} (-5.32,0);
    \draw[gb] (0,0) node[wh]{1} -- node[wh]{361} (14.4 ,0);

Refactoring a little

Refactoring is the step of gradually changing and expanding existing code from "what's there" to "what it shall become". Usually you incorporate at least:

  • a pass/fail test (compile and visual inspection here)
  • generalizing
  • anticipating

If you use tikzlibrary calc you need to:

  • define a unit length, or maximum or whatever pleases you or is useful to do
  • switch to math-mode for coordinate calculations (see ch. 13.5)

My anticipation here is:

  • the "1" in the middle is arbitrary (i.e. unspoken, if it could be a stacked 10 or whatever)
  • so moving it out
  • preparing the rest for argument passing, e.g. via \pic

(Same screenshot)

\usetikzlibrary{calc}           % <<< NEW

% ~~~ define unit ~~~~~~~~
\newcommand\myunit[0]{0.04}     % <<< NEW

    lw/.style={line width=12mm},
    rb/.style={lw,red },
    \draw[rb] (0,0)     -- node[wh]{148} ($\myunit*(-148,0)$);
    \draw[gb] (0,0)     -- node[wh]{361} ($\myunit*( 361,0)$);
    \node[wh] at (0,0) {1};
  • 1
    ahh cool thank you do you know how to do this automatic
    – Lucas
    Feb 29 at 7:51
  • @Lucas, thank you. What do you mean by automatic?
    – MS-SPO
    Feb 29 at 8:08
  • How you can automate it so that when you change the numbers, the division of the bar chart also changes
    – Lucas
    Feb 29 at 8:52
  • @Lucas, please see the new section in the answer. You do read and follow my advice, don't you? tex.stackexchange.com/questions/711738/… // "(...) (or use the calc-lib) (...)"
    – MS-SPO
    Feb 29 at 10:02

This is another solution in which the size of the diagram remains the same but the scaling is "automatically" adjusted depending on the number. %This is where the data is entered

%size of the diagram\\
%All data entered will be added together here\\
\FPeval{\ga}{clip(round(\a + \b + \c , 2))}\\
% hier wird die Länge durch die addierten Daten geteilt\\
\FPeval{\ml}{clip(round(\gl / \ga , 2))}\\
% Here the lengths of the respective section are calculated\\
\FPeval{\la}{clip(round(\ml * \a , 2))}\\
\FPeval{\lb}{clip(round(\ml * \b + \la, 2))}\\
\FPeval{\lc}{clip(round(\ml * \c +\lb, 2))}\\  
lw/.style={line width=12mm},\\
rb/.style={lw, red},\\
g/.style={lw, gray},\\
dg/.style={lw, darkgray},\\
% The calculated coordinates are automatically entered here \\
\draw[rb] ( 0, 0)             -- node[wh]{\a} (\la ,0);\\
\draw[g] (\la,0)             -- node[wh]{\b} (\lb ,0);\\
\draw[dg] (\lb,0)             -- node[wh]{\c} (\lc ,0);\\
% If the number at b is single-digit you should include 2 and 3 in a comment % and remove the % sign at 4\\
%\draw[gb] (\la,0) node[wh]{\b} -- node[wh]{\c} (\lc ,0);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .