Grouped bar chart from csv or xlsx

I want to make a grouped bar chart in LaTeX, using data from a csv (or xlsx) file. I want it to be styled similarly to my other bar charts. I don't know how to get it into the right format and everytime I try to adjust grouped bar chart examples in to my style, I get errors. This is the bar chart from my data (made it in Excel as placeholder):

And this the style for the other bar charts in my thesis (ignore the line, that shouldn't be in the grouped bar chart):

What I would like:

• A grouped bar chart as in the Excel example
• The same bar and legend style as the second picture
• The same axis label style as in the second picture (instead of 17 to 29, 28-5 to 5-6)

My code is:

\PassOptionsToPackage{table,dvipsnames,svgnames}{xcolor}
\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}

\usepackage{pgfplots}
\pgfplotsset{
compat=1.16,
% created a style for the common axis' options
my axis style/.style={
width=\linewidth,
height=0.35\linewidth,
bar width=0.9,
enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
ymin=0,
legend style={
anchor=south,       % <-- changed from north'
legend columns=2,
},
ylabel={PR\textsubscript{A}},
xtick=data,
axis lines*=left,
ymajorgrids,
%
table/x=x,
},
%made the beginnings of a second axis style, because I figured it needs to be different for grouped
my second axis style/.style={
width=\linewidth,
height=0.35\linewidth,
bar width=0.3,
enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
ymin=0,
legend style={
anchor=south,       % <-- changed from north'
legend columns=2,
},
ylabel={PR\textsubscript{A}},
xtick=data,
axis lines*=left,
ymajorgrids,
%
table/x=x,
},
% created a style for the common ybar' options
my ybar style/.style={
ybar,
my ybar legend,            % <-- change legend image accordingly
#1!50!black,
fill=white!70!black,
nodes near coords,      % <-- moved from axis' options here
% state absolute positions for the nodes near coords'
scatter/position=absolute,
node near coords style={
% state where the nodes should appear
at={(\pgfkeysvalueof{/data point/x},0.5)},
anchor=center,
% make the font a bit smaller
font=\footnotesize,
% set the number format of the nodes near coords'
/pgf/number format/.cd,
fixed,
precision=2,
zerofill,
},
},
my ybar legend/.style={
/pgfplots/legend image code/.code={
\draw [
##1,
/tikz/.cd,
yshift=-0.25em,
] (0cm,0cm) rectangle (3pt,0.8em);
},
},
}

%data for the grouped bar chart
x          SP_cSi_2_3   SP_cSi_2_4  Reference
1   0.500   0.627   0.868
2   0.781   0.778   0.859
3   0.819   0.868   0.871
4   0.732   0.824   0.876
5   0.853   0.873   0.954
6    0.813   0.838   0.940
7    0.712   0.759   0.876
8    0.864   0.894   0.887
9    0.465   0.614   0.891

\begin{document}

\begin{figure}
\begin{tikzpicture}
\begin{axis}[my second axis style,
ybar,
enlargelimits=0.1,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={PR\textsubscript{A}},
xtick=data,
]
\legend{SP cSi 2.3,SP cSi 2.4,ref}
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}


Which gives me:

Edit:

Thanks to Marmot, I'm very close now. My new code:

\PassOptionsToPackage{table,dvipsnames,svgnames}{xcolor}
\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}
%\usepackage{showframe}

\usepackage{pgfplots}
\pgfplotsset{
compat=1.16,
%made the beginnings of a second axis style, because I figured it needs to be different for grouped
my second axis style/.style={
width=\linewidth,
height=0.35\linewidth,
bar width=0.2, %<- changed
enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
ymin=0,
legend style={
anchor=north,       % <-- changed from north'
legend columns=3,
},
ylabel={PR\textsubscript{A}},
xtick=data,
axis lines*=left,
ymajorgrids,
%
table/x=x,
},
% created a style for the common ybar' options
my second ybar style/.style={
ybar,
my ybar legend,            % <-- change legend image accordingly
#1!50!black,
fill=white!70!black,, %<- changed back
nodes near coords,      % <-- moved from axis' options here
% state absolute positions for the nodes near coords'
scatter/position=absolute,
node near coords style={
% state where the nodes should appear
at={(\pgfkeysvalueof{/data point/x},0.5*\pgfkeysvalueof{/data point/y})},
% make the font a bit smaller
font=\footnotesize,
% set the number format of the nodes near coords'
/pgf/number format/.cd,
fixed,
precision=2,
zerofill,
},
},
my ybar legend/.style={
/pgfplots/legend image code/.code={
\draw [
##1,
/tikz/.cd,
yshift=-0.25em,
] (0cm,0cm) rectangle (3pt,0.8em);
},
},
}

%data for the grouped bar chart
x          SP_cSi_2_3   SP_cSi_2_4  Reference
28-5   0.500   0.627   0.868
29-5   0.781   0.778   0.859
30-5   0.819   0.868   0.871
31-5   0.732   0.824   0.876
1-6   0.853   0.873   0.954
2-6    0.813   0.838   0.940
3-6    0.712   0.759   0.876
4-6    0.864   0.894   0.887
5-6    0.465   0.614   0.891

\begin{document}

\begin{figure}
\begin{tikzpicture}
\begin{axis}[my second axis style,
ybar,
ylabel={PR\textsubscript{A}},
xtick= data,
]
\legend{Floating 2.3~~ , Floating 2.4~~ , Reference}
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}


Final question that remains:

• Change the x ticks to the dates in the excel example (I get errors when I change them to {28-5, 29-5, etc.} in the \loadedtable)
• Zero fill the y-axis label (as in the excel example)

Any ideas?

• Thank you for posting an example! At the same time, much of your code does not seem to be relevant to the question you're asking here. Please limit the example to only the code required for your issue to appear. You can have a look at this guide for how to prune your code for this purpose. Furthermore, your example does not compile correctly, can you fix this please? (To do so, create a new folder and create a new tex file in it containing this code - if this code does not produce any errors, post it again by editing your question.) – Tom Jul 30 '18 at 15:19
• I changed the code, so now it compiles properly. I think I removed all the unnecessary stuff from the preamble too. Most of it is copied from other people who helped me, because I'm still a bit of a LaTeX noob – Hans Jul 30 '18 at 15:45

I guess that one reason why no one dared to answer this nice question so far is that some of the things that you do seem to go against what you want to achieve. So I post a proposal here and you could perhaps use it in order to explain in more detail what you want to change. I indicated the changes in the code.

\PassOptionsToPackage{table,dvipsnames,svgnames}{xcolor}
\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}

\usepackage{pgfplots}
\pgfplotsset{
compat=1.16,
% created a style for the common axis' options %<- removed since not used
%made the beginnings of a second axis style, because I figured it needs to be different for grouped
my second axis style/.style={
width=\linewidth,
height=0.35\linewidth,
bar width=0.15, %<- changed
enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
ymin=0,
legend style={
anchor=south,       % <-- changed from north'
legend columns=2,
},
ylabel={PR\textsubscript{A}},
xtick=data,
axis lines*=left,
ymajorgrids,
%
table/x=x,
},
% created a style for the common ybar' options
my ybar style/.style={
ybar,
my ybar legend,            % <-- change legend image accordingly
#1!50!black,
fill=#1, %<- changed
nodes near coords,      % <-- moved from axis' options here
% state absolute positions for the nodes near coords'
scatter/position=absolute,
node near coords style={
% state where the nodes should appear
at={(\pgfkeysvalueof{/data point/x},0.5*\pgfkeysvalueof{/data point/y})},
% make the font a bit smaller
font=\footnotesize,
% set the number format of the nodes near coords'
/pgf/number format/.cd,
fixed,
precision=2,
zerofill,
},
},
my ybar legend/.style={
/pgfplots/legend image code/.code={
\draw [
##1,
/tikz/.cd,
yshift=-0.25em,
] (0cm,0cm) rectangle (3pt,0.8em);
},
},
}

%data for the grouped bar chart
x          SP_cSi_2_3   SP_cSi_2_4  Reference
1   0.500   0.627   0.868
2   0.781   0.778   0.859
3   0.819   0.868   0.871
4   0.732   0.824   0.876
5   0.853   0.873   0.954
6    0.813   0.838   0.940
7    0.712   0.759   0.876
8    0.864   0.894   0.887
9    0.465   0.614   0.891

\begin{document}

\begin{figure}
\begin{tikzpicture}
\begin{axis}[my second axis style,
ybar,
enlargelimits=0.1,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ylabel={PR\textsubscript{A}},
xtick=data,
]