I am trying to create a stacked bar chart above a table, basically identical to the one below.

Example of what I am after

I have found similar posts such as: How can I mix an "ybar" and an "ybar stacked" with pgfplots?

However, my experience is very limited to simple tables and I am struggling to modify this to suit my needs.

Any ideas on how I can achieve such a graph?


  • Welcome to TeX.SX! Please help us (and also you) and add a minimal working example (MWE), that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. – Bobyandbob May 25 '17 at 9:02

Well, I have other things to do, so why not procrastinate. Here's a possibility where the "table" is built manually as a series of individual \nodes. The bar chart is made with pgfplots, and the table is made afterwards by looping over the data table. The bar width and separation is calculated based on the width of the axis and the total number of bars.

enter image description here


% for sans serif ticks (https://tex.stackexchange.com/questions/33325/)

% read in data
x v nv
0 59 29
0.01 40 70
0.02 53 81
0.03 60 83
0.04 76 82
0.05 78 86
0.10 119 88
0.20 210 89
0.30 376 89
0.40 508 90
0.50 605 91
0.60 654 91
0.70 705 91
0.80 766 91
0.90 856 91
1.00 1026 91

% get number of rows
% subtract 1 because table indices start at 0
% for convenience, macro to store width of axis

  cell/.style={ % style used for "table" cells
    minimum width={\MyAxisW/(\Nrows+1)}, % +1 because -1 above
    minimum height=4ex,
    inner sep=0pt,
    outer sep=0pt,
    anchor=north west,
   % so axis labels and ticklabels are not accounted for in size settings   
   scale only axis,
   % use a stacked bar char
   ybar stacked,
   % we add the ticklabels as part of the table, so no xticks needed
   % set distance between yticks
   ytick distance=200,
   % and add grid lines
   % only need left y-axis line
   axis y line=left,
   x axis line style={draw=none},
   % divide axis width by twice the number of rows, so that the whitespace between
   % bars is the same as the bar width ...
   bar width={\MyAxisW/(2*\Nrows+2)},
   % and for that we need to make sure that the distance from the first/last tick
   % to the axis edge is the same, so that there is a half a bar width of space
   enlarge x limits={abs={\MyAxisW/(2*\Nrows+2)}},
   ylabel={Number of interactions},
   % move xlabel to below table
   xlabel shift=12ex,
   % set yticks as sans serif
   tick label style={
    % and remove comma in 1,000
    /pgf/number format/set thousands separator=},
   % set axis labels as sans serif
   label style={font=\sansmath\sffamily}
  % because the x-values are not evenly spaced, used index as x-value instead
  \addplot +[black!60] table[x expr=\coordindex,y=nv] {\datatable};

  \addplot +[black!20] table[x expr=\coordindex,y=v] {\datatable};

% define a starter coordinate at the lower left corner of the axis
\coordinate (c-0-0) at (ax.south west);

% loop over the table
\foreach [count=\j from 1] \i in {0,...,\Nrows}
  % get element \i from the x-column, stored in \pgfplotsretval
  % add node with value
  \node [cell] (c-0-\j) at (c-0-\i.north east) {\pgfplotsretval};
  % repeat for other two columns
  \node [cell] (c-1-\j) at (c-0-\j.south west) {\pgfplotsretval};
  \node [cell] (c-2-\j) at (c-1-\j.south west) {\pgfplotsretval};

% add "legend" on the left
\matrix [draw,nodes={cell,draw=none},anchor=north east,row sep=0pt,outer sep=0pt,inner ysep=0pt] (m) at (c-1-1.north west)
 \node {\ref{dataV}};  & \node{Non-validated}; \\
 \node {\ref{dataNV}}; & \node{Validated}; \\

% draw center line of legend
\draw (m.west) -- (m.east);


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.