My goal is to generate this bar chart:

I was able to achieve this chart as my MWE:
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.6}
\usepackage{xifthen}
\usetikzlibrary{patterns}
\usepackage{filecontents}
\makeatletter
\pgfplotsset{
/pgfplots/flexible xticklabels from table/.code n args={3}{%
\pgfplotstableread[#3]{#1}\coordinate@table
\pgfplotstablegetcolumn{#2}\of{\coordinate@table}\to\pgfplots@xticklabels
\let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x
}
}
\makeatother
% argument #1: any options
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\csname pgfplots@init@cleared@structures\endcsname
\pgfplotsset{#1}%
}{%
% draws the legend:
\csname pgfplots@createlegend\endcsname
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
\begin{filecontents}{testdata.csv}
matrix P methodAfirstPhase methodAsecondPhase methodBfirstPhase methodBsecondPhase
a 2 3 7 2 5
a 4 1 4 1 3
b 2 2 6 1 5
b 4 1 2 1 1
\end{filecontents}
\begin{document}
\begin{tikzpicture}[remember picture]
\def\numpsPLOT{2}
\def\nummtx{2}
\foreach \matrixid/\P/\axisState/\barShift/\Pshift in {0/2//0/0, 0/4/hide axis/30/.1, 1/2/hide axis/70/.3, 1/4/hide axis/100/.4} {
\pgfkeys{/pgf/number format/.cd,fixed,precision=0}%
\pgfmathparse{\matrixid*\numpsPLOT-1}%
\pgfmathtruncatemacro\le{\pgfmathresult+1}%
\pgfmathparse{\matrixid*\numpsPLOT+\numpsPLOT}%
\pgfmathtruncatemacro\us{\pgfmathresult}%
\pgfmathparse{\nummtx*\numpsPLOT}%
\pgfmathtruncatemacro\ue{\pgfmathresult}%
\begin{axis}
[ ybar stacked,
\axisState,
bar shift=\barShift-100,
enlarge x limits=9,
ymin=0,ymax=11,
x filter/.code={\expandafter\ifnum \thisrow{P}=\P\else\def\pgfmathresult{}\fi},
flexible xticklabels from table={testdata.csv}{matrix}{}, %ignore chars={\%,\_}
x tick label style={font=\small,text width=1.7cm,align=center,rotate=45},
xtick=data,
nodes near coords*,
]
\addplot[black,fill=black!20] table
[ x expr=\coordindex,
y=methodAfirstPhase,
skip coords between index={-1}{\le}, skip coords between index={\us}{\ue}
] {testdata.csv};
\addplot[black,pattern=north west lines,] table
[ x expr=\coordindex,
y=methodAsecondPhase,
skip coords between index={-1}{\le}, skip coords between index={\us}{\ue}
] {testdata.csv};
\end{axis}
\begin{axis}
[ ybar stacked,
\axisState,
bar shift={\barShift+10-100},
enlarge x limits=9,
ymin=0,ymax=11,
x filter/.code={\expandafter\ifnum \thisrow{P}=\P\else\def\pgfmathresult{}\fi},
flexible xticklabels from table={testdata.csv}{matrix}{}, %ignore chars={\%,\_}
x tick label style={font=\small,text width=1.7cm,align=center,rotate=45},
xtick=data,
nodes near coords*,
]
\addplot[black,fill=black!40] table
[ x expr=\coordindex,
y=methodBfirstPhase,
skip coords between index={-1}{\le}, skip coords between index={\us}{\ue}
] {testdata.csv};
\addplot[black,fill=black!40,pattern=north east lines,] table
[ x expr=\coordindex,
y=methodBsecondPhase,
skip coords between index={-1}{\le}, skip coords between index={\us}{\ue}
] {testdata.csv};
\end{axis}
\node[below left] at (rel axis cs:0.65+\Pshift,1) {P=\P};
}
\end{tikzpicture}
\begin{tikzpicture}
\begin{customlegend}[legend columns=2,legend style={align=left,draw=none,column sep=2ex},legend entries={Method A $\Phi1$,Method B $\Phi1$,Method A $\Phi2$,Method B $\Phi2$}]
\addlegendimage{black,fill=black!20,area legend}
\addlegendimage{black,fill=black!40,area legend}
\addlegendimage{black,fill=black!50,area legend,pattern=north west lines,}
\addlegendimage{black,fill=black!0,area legend,pattern=north east lines,}
\end{customlegend}
\end{tikzpicture}
\end{document}
Outcome:

Here what I want:

There are three problems:
1) I want names of matrices to be put below related bars. I cannot use symbolic x coords={a,b} since matrix names must be loaded from data file. In my MWE, only matrix name a is visible, but b is missing. I shift bars via bar shift=... but I cannot shift x ticks. Is there any way to shift x ticks?
2) I cannot put numbers inside the bars. Matrix names and numbers must be shifted as the bar shifted via bar shift=.... (Same problem) Again note that I cannot use symbolic x coords={a,b} since matrix names are determined dynamically according to data file.
3) I want to put processor numbers according to bars and just above or preferably below the corresponding bar. In my MWE, I have put processor numbers manually. I want to automatically put according to related bar.
In my original code; number of processors, matrix names, number of matrices, ... everything may change so my code should be flexible.


