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.
Best Answer
You should specify a
y
value with absolute dimensions (likey=8mm
), where the value should be equivalent to thebar width
plus the gap you want between the bars. So if yourbar width
is6mm
,y=8mm
will give you a2mm
gap between the bars. To get the same gap between the x axis and the bars, you can addenlarge y limits={abs=<value>}
, where<value>
should be0.5 + 0.5*(y - bar width) / y
, so in this case0.5 + 0.5 * (8-6)/8 = 0.625
. If you only want half the gap between the bars and the axis, setenlarge y limits={abs=0.5}
.