[Tex/LaTex] pgfplots stacked bar plot: distance to axis, padding between bars

pgfplotstikz-pgf

I have the following chart:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}

\definecolor{transfertoserver}{HTML}{D7191C}
\definecolor{database}{HTML}{FDAE61}
\definecolor{transfertoclient}{HTML}{ABDDA4}
\definecolor{rendering}{HTML}{2B83BA}


\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[xbar stacked,
legend style={legend columns=4,at={(0,-0.35)},anchor=north west,draw=none},
ytick={0,1},
axis y line*=none,
axis x line*=bottom,
tick label style={font=\footnotesize},
legend style={font=\footnotesize},
label style={font=\footnotesize},
xtick={0,5,10,15,20,25},
width=.8\textwidth,
height=4cm,
bar width=6mm,
xlabel={Time in Seconds},
yticklabels={Database Optimizations, Reference Timing},
xmin=0,
xmax=25,
area legend,
enlarge y limits=0.3,
]
\addplot[transfertoserver,fill=transfertoserver] coordinates
% Transfer
{(0.38,0) (0.38,1)};
\addplot[database,fill=database] coordinates
% Database
{(2.4,0) (9.66,1)};
\addplot[transfertoclient,fill=transfertoclient] coordinates
% Transfer
{(0.23,0) (0.23,1)};
\addplot[rendering,fill=rendering] coordinates
% Rendering
{(14.66,0) (14.66,1)};
\legend{Transfer,Database,Transfer,Rendering}
\end{axis}
\end{tikzpicture}
\caption{Performance Benefit by Database Optimizations}
\label{fig:performance:database}
\end{figure}
\end{document}

However I would like to have a few milimeters between the x axis and the bottom bar. I can get this with enlarge y limits=0.2. But then I have to find a matching factor for every figure. I want to define an absolute measure. The other thing is how can I get the bars closer to each other?

Doing this in one question to not spam too much.

Screenshot of the PDF, showing the huge space

Best Answer

You should specify a y value with absolute dimensions (like y=8mm), where the value should be equivalent to the bar width plus the gap you want between the bars. So if your bar width is 6mm, y=8mm will give you a 2mm gap between the bars. To get the same gap between the x axis and the bars, you can add enlarge y limits={abs=<value>}, where <value> should be 0.5 + 0.5*(y - bar width) / y, so in this case 0.5 + 0.5 * (8-6)/8 = 0.625. If you only want half the gap between the bars and the axis, set enlarge y limits={abs=0.5}.

stacked bar chart

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}

\definecolor{transfertoserver}{HTML}{D7191C}
\definecolor{database}{HTML}{FDAE61}
\definecolor{transfertoclient}{HTML}{ABDDA4}
\definecolor{rendering}{HTML}{2B83BA}


\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
    xbar stacked,
    legend style={
        legend columns=4,
        at={(xticklabel cs:0.5)},
        anchor=north,
        draw=none
    },
    ytick=data,
    axis y line*=none,
    axis x line*=bottom,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    xtick={0,5,10,15,20,25},
    width=.8\textwidth,
    bar width=6mm,
    xlabel={Time in Seconds},
    yticklabels={Database Optimizations, Reference Timing, Something Else},
    xmin=0,
    xmax=25,
    area legend,
    y=8mm,
    enlarge y limits={abs=0.625},
]
\addplot[transfertoserver,fill=transfertoserver] coordinates
% Transfer
{(0.38,0) (0.38,1) (5,2)};
\addplot[database,fill=database] coordinates
% Database
{(2.4,0) (9.66,1)(5,2)};
\addplot[transfertoclient,fill=transfertoclient] coordinates
% Transfer
{(0.23,0) (0.23,1)(5,2)};
\addplot[rendering,fill=rendering] coordinates
% Rendering
{(14.66,0) (14.66,1)(5,2)};
\legend{Transfer,Database,Transfer,Rendering}
\end{axis}
\end{tikzpicture}
\caption{Performance Benefit by Database Optimizations}
\label{fig:performance:database}
\end{figure}
\end{document}