# Exporting stacked bars using matlab2tikz in MATLAB working strangely

I have created a stacked bars in matlab as shown in the figure. In matlab figure window it looks like this:

I use the following command to get this plot.

plotBarStackGroups(rand(5,3,3), [1:3])


It basically plots 3 stacked bars together, with 3 data each, into 5 groups.

I use plotBarStackGroups function from matlab file exchange to plot such a figure. The function for reference:

function [] = plotBarStackGroups(stackData, groupLabels)
NumGroupsPerAxis = size(stackData, 1);
NumStacksPerGroup = size(stackData, 2);
groupBins = 1:NumGroupsPerAxis;
MaxGroupWidth = 0.65; % Fraction of 1. If 1, then we have all bars in groups touching
groupOffset = MaxGroupWidth/NumStacksPerGroup;
figure
hold on;
xtick = [];
for i=1:NumStacksPerGroup
Y = squeeze(stackData(:,i,:));
internalPosCount = i - ((NumStacksPerGroup+1) / 2);
groupDrawPos = (internalPosCount)* groupOffset + groupBins;
h(i,:) = bar(Y, 'stacked');
set(h(i,:),'BarWidth',groupOffset);
set(h(i,:),'XData',groupDrawPos);
xtick = [xtick; groupDrawPos(:)];
end
hold off;
set(gca,'XTickMode','manual');
xts = sort(xtick);
set(gca,'XTickLabelMode','manual','XTick',xts,'XTickLabel',groupLabels);
end


Then I export this figure using matlab2tikz into a .tex file and include it my report.

matlab2tikz('filename','stackedplot.tex')


My latex file looks like this.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz,ifthen,pgfplots}
\usepackage{pgffor}

\begin{document}
\input{stackedplot.tex}
\end{document}


The results looks something like this :

The stacked bars seems to move up. I do not know why. Any help is appreciated.

• we need to see the tex file and the data. Matlab function won't do much help. – percusse Apr 20 '15 at 17:45
• I meant the content of stackedplot.tex – percusse Apr 20 '15 at 17:53
• @percusse I have included the tex file now. For the data, you can use the rand function. I hope the problem is reproducible now. – Adithyan Ilangovan Apr 20 '15 at 17:54

I posted the same question in GitHub. One of them came up with a workaround.

The problems seems to be that bar plots cannot handle both stacked and grouped at the same time. The workaround is to manipulate the bar plots in the matlab.

A = rand(5,3,4);

subplot(211)
plotBarStackGroups(A,1:4)

subplot(212)
sz = size(A);
B = NaN((sz(1)+1)*sz(2)-1,sz(3));
for ii = 1:sz(1)
pos = (ii-1)*(sz(2)+1)+1:ii*(sz(2)+1)-1;
B(pos,:) = A(ii,:,:);
end
bar(B,'stacked')

% Or equivalently
C = [A, NaN(sz(1),1,sz(3))];
C = reshape(permute(C, [2,1,3]),sz(1)*(sz(2)+1),sz(3));
bar(C(1:end-1,:),'stacked')


Result:

Now if we export this using matlab2tikz and then include it in .tex file, it looks like this:

So I guess it is a limitation of pgfplots. Thanks to Oleg Komarov from GitHub.

If somebody else faces a similar problem, I hope it helps.

• If you can submit your actual tex file we can modify it to make it work. That statement is not correct that it cannot handle groups and bar plots. neither matlab part nor your actual TeX file is important here. – percusse Apr 21 '15 at 8:41