12

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

2
  • While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Mar 14, 2012 at 16:21
  • I have created the MWE now.
    – Felix
    Mar 14, 2012 at 19:50

1 Answer 1

16

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}
3
  • Thanks, that looks amazing. I think for the abs= there is a newer version of pgfplots needed than included with TeXLive 2009 as shipped on Ubuntu. But the y= helped me alot. Your positing of the legend is better two. I think the bottom line should have an arrow on the right side ... Thanks alot!
    – Felix
    Mar 14, 2012 at 21:42
  • @Jake: When I try to run the given example, I get Package pgfplots Error: Sorry, pgfplots expects stacked plots to have exactly the same number of coordinates By the way: Always when I try to find how to do something new in LaTeX, I find your answers, Jake. Thank you very much! Mar 12, 2013 at 19:44
  • 3
    Ah, it was the newline after % Transfer Mar 12, 2013 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .