# pgfplots: drawing areas from a table starting “above” an axis

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}

size        min         max         med
1           1           3           2
10          2           4           3

\begin{tikzpicture}
\begin{axis}[
legend entries={above median,below median, median,},
ymin=0,
area style,
]

\end{axis}
\end{tikzpicture}

\end{document}

-
Welcome to TeX.sx! –  lockstep Dec 13 '12 at 15:32

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}

size        min         max         med
1           1           3           2
10          2           4           3

\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;

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