Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Peter Grill Mar 14 '12 at 16:21
    
I have created the MWE now. –  Felix Mar 14 '12 at 19:50

1 Answer 1

up vote 10 down vote accepted

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}.

\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}
share|improve this answer
    
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 '12 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! –  moose Mar 12 '13 at 19:44
1  
Ah, it was the newline after % Transfer –  moose Mar 12 '13 at 19:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.