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.

Please let me explain the motivation behind this a bit more broadly. I'd like to draw measured benchmark values as follows: background/nothing between the x-axis and the minimum value a green area between the minimum value and the median a black line at the median a red area between the median and the maximum value background/nothing above the maximum

I tried various attempts to reach this before using the IMHO quite inelegant way of just drawing a white area on top of the other plots (although after all the pain I think I can live with that :), the code below shows that and how an alternative solution should look like too.

One try was to use stacked plots, but of course the values are stacked and I could not find an equivalent to bar shift=0pt. I also think there is no way to disable the stacking per plot (or the other way around, i.e. disable it globally but enable it for selected plots).

The most similar solution I could find yet is Fill between two curves in pgfplots. but I could not get it to work with the pre-existing table data I have (is there really no easy way to copy selected columns into a new table!?). Is the white area approach below really the best way to do this?

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{
    size        min         max         med
    1           1           3           2
    10          2           4           3
}{\loadedtable}

\begin{tikzpicture}
\begin{axis}[
    legend entries={above median,below median, median,},
    ymin=0,
    area style,
    ]
    \addplot [fill=red!40, draw=none]   table [x=size, y=max]   {\loadedtable} \closedcycle;
    \addplot [fill=green!40, draw=none] table [x=size, y=med]   {\loadedtable} \closedcycle;
    \addplot [draw=black, line legend]  table [x=size, y=med]   {\loadedtable};
    \addplot [fill=white, draw=white]   table [x=size, y=min]   {\loadedtable} \closedcycle;

\end{axis}
\end{tikzpicture}

\end{document}
share|improve this question
2  
Welcome to TeX.sx! –  lockstep Dec 13 '12 at 15:32
add comment

1 Answer 1

up vote 4 down vote accepted

You can use stack plots=y if you set subtract the minimum value from the median value and the median value from the maximum value by using y expr=\thisrow{med}-\thisrow{min} and y expr=\thisrow{max}-\thisrow{med}.

Stacking of plots can be applied to selected plots by only providing stack plots=y to the desired plots, or by setting stack plots=false for the plots you don't want to stack.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{
    size        min         max         med
    1           1           3           2
    10          2           4           3
}{\loadedtable}

\begin{tikzpicture}
\begin{axis}[
    enlarge x limits=false, axis on top,
    legend entries={below median,median, above median},
    reverse legend, legend pos=outer north east,
    ymin=0,
    ]
    \addplot [stack plots=y, fill=none, draw=none, forget plot]   table [x=size, y=min]   {\loadedtable} \closedcycle;
    \addplot [stack plots=y, fill=green!40, draw opacity=0, area legend] table [x=size, y expr=\thisrow{med}-\thisrow{min}]   {\loadedtable} \closedcycle;
    \addplot [stack plots=false, draw=black, thick]  table [x=size, y=med]   {\loadedtable};
    \addplot [stack plots=y, fill=red!40, draw opacity=0, area legend]   table [x=size, y expr=\thisrow{max}-\thisrow{med}]   {\loadedtable} \closedcycle;

   % 

\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
1  
excellent, thank you! (and now I feel pretty stupid especially for not joining stackexchange earlier ;) –  stefanct Dec 13 '12 at 16:41
    
@stefanct: Glad it helps (and welcome to the site)! If you consider your question answered, you can mark it as such by clicking the tick mark next to the answer. –  Jake Dec 13 '12 at 16:52
1  
I was about to do that of course but got distracted by enter not adding line breaks but submitting my unfinished comment. ;) I wanted to add this: The reverse legend, axis on top, forget plot and opacity=0 directives are all very useful in this context too and unknown to me yet, so thanks for these hints too. –  stefanct Dec 13 '12 at 16:59
add comment

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.