I have the following data:
Year upper lower
2007 3.2 -.81
2008 2.0 -.29
2009 1.8 -.95
2010 -.5 -3.9
2011 -2. -7.3
And I'd like a graph that fills the area between upper and lower. The effect I'm going for is similar to Fill between two curves in pgfplots..
First question: If my data were organized as below, I think I could easily just use a fill command:
Year upper
2007 3.2
2008 2.0
2009 1.8
2010 -.5
2011 -2.
2011 -7.3
2010 -3.9
2009 -.95
2008 -.29
2007 -.81
Is there a way to use pgfplots, or any other tool, to plot the graph with shading between upper and lower boundary in this data format?
Second question: Also, I would like the shaded region between years 2009.5 and 2010.5 to have a different color. Is there a way to do this without having these points in my data?
After cobbling together solutions to other questions on this site, the closest I've come is the below minimal non-working example. I'm able to create macros for the new interpolated values, and I hoped to concatenate those into the existing table, but was unable to use the macros in the tableread command. I'm still woefully short of my goal, but this is advanced stuff for me.
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{calc}
\pgfplotstableread{
Year upper lower
2007 3.2 -.81
2008 2.0 -.29
2009 1.8 -.95
2010 -.5 -3.9
2011 -2. -7.3
}\datatable
\begin{document}
% Interpolate 2009.5 upper
\pgfplotstablegetelem{2}{lower}\of\datatable
\pgfmathsetmacro{\y}{\pgfplotsretval}
\pgfplotstablegetelem{3}{lower}\of\datatable
\pgfmathsetmacro{\x}{\pgfplotsretval}
\pgfmathsetmacro{\firstupper}{\y-(\y-\x)/2}
% Interpolate 2009.5 lower
\pgfplotstablegetelem{2}{upper}\of\datatable
\pgfmathsetmacro{\y}{\pgfplotsretval}
\pgfplotstablegetelem{3}{upper}\of\datatable
\pgfmathsetmacro{\x}{\pgfplotsretval}
\pgfmathsetmacro{\firstlower}{\y-(\y-\x)/2}
% Interpolate 2010.5 upper
\pgfplotstablegetelem{3}{lower}\of\datatable
\pgfmathsetmacro{\y}{\pgfplotsretval}
\pgfplotstablegetelem{4}{lower}\of\datatable
\pgfmathsetmacro{\x}{\pgfplotsretval}
\pgfmathsetmacro{\firstupper}{\y-(\y-\x)/2}
% Interpolate 2010.5 lower
\pgfplotstablegetelem{3}{upper}\of\datatable
\pgfmathsetmacro{\y}{\pgfplotsretval}
\pgfplotstablegetelem{4}{upper}\of\datatable
\pgfmathsetmacro{\x}{\pgfplotsretval}
\pgfmathsetmacro{\secondlower}{\y-(\y-\x)/2}
\begin{tikzpicture}
\begin{axis}[
/pgf/number format/1000 sep={},
xmin=2007,
xmax=2011,
ymin=-8,
ymax=5,
enlarge x limits=false
]
\addplot [draw=none, stack plots=y,forget plot] table [y=lower] {\datatable};
\addplot [draw=none, fill=gray, opacity=.5,stack plots=y] table [y expr=\thisrow{upper}-\thisrow{lower}] {\datatable} \closedcycle;
\addplot [stack plots=y, stack dir=minus,forget plot, draw=none] table [y expr=\thisrow{upper}-\thisrow{lower}] {\datatable};
\end{axis}
\begin{axis}[
/pgf/number format/1000 sep={},
xmin=2007,
xmax=2011,
ymin=-8,
ymax=5,
enlarge x limits=false
]
\addplot [draw=none, stack plots=y, forget plot, restrict x to domain=2009.5:2010.5] table [y=lower]{\datatable};
\addplot [draw=none, fill=white,opacity=.5,stack plots=y, restrict x to domain=2009.5:2010.5] table [y expr=\thisrow{upper}-\thisrow{lower}] {\datatable} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
Thanks!


