# How to plot anomaly using two colors in a single plot with pgfplot?

I want to create a line plot using a table data included in the MWE. The thing is that, using this data, I want that the plot looks like the next figure:

i.e., with different color areas for one plot.

If that doesn't work the alternative choice for making the plot is like the next figure:

But in this case, the 0 is the changing color line instead of median. The point is to give red color to positive values and blue color to negative values in just one plot. But also adding a color bar to indicate anomaly (not legend).

Here's my MWE for the figure:

\documentclass[]{article}
\usepackage{filecontents,pgfplots}

\begin{filecontents}{data.dat}
year DJF JFM FMA MAM AMJ MJJ JJA JAS ASO SON OND NDJ
1950 -1.4 -1.2 -1.1 -1.2 -1.1 -0.9 -0.6 -0.6 -0.5 -0.6 -0.7 -0.8
1951 -0.8 -0.6 -0.2 0.2 0.2 0.4 0.5 0.7 0.8 0.9 0.7 0.6
1952 0.5 0.4 0.4 0.4 0.4 0.2 0 0.1 0.2 0.2 0.2 0.3
1953 0.5 0.6 0.7 0.7 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.7
1954 0.7 0.4 0 -0.4 -0.5 -0.5 -0.5 -0.7 -0.7 -0.6 -0.5 -0.5
1955 -0.6 -0.6 -0.7 -0.7 -0.7 -0.6 -0.6 -0.6 -1.0 -1.4 -1.6 -1.4
1956 -0.9 -0.6 -0.6 -0.5 -0.5 -0.4 -0.5 -0.5 -0.4 -0.4 -0.5 -0.4
1957 -0.3 0 0.3 0.6 0.7 0.9 1.0 1.2 1.1 1.2 1.3 1.6
1958 1.7 1.5 1.2 0.8 0.7 0.6 0.5 0.4 0.4 0.5 0.6 0.6
1959 0.6 0.5 0.4 0.2 0.1 -0.2 -0.3 -0.3 -0.1 -0.1 -0.1 -0.1
1960 -0.1 -0.2 -0.1 0 -0.1 -0.2 0 0.1 0.2 0.1 0 0
1961 0 0 -0.1 0 0.1 0.2 0.1 -0.1 -0.3 -0.3 -0.2 -0.2
1962 -0.2 -0.2 -0.2 -0.3 -0.3 -0.2 -0.1 -0.2 -0.2 -0.3 -0.3 -0.4
1963 -0.4 -0.2 0.1  0.2 0.2 0.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1.1 -0.9 -0.7 -0.7 -0.6 -0.5 -0.6 -0.7 -0.8 -0.8
2001 -0.7 -0.6 -0.5 -0.3 -0.2 -0.1 0 -0.1 -0.1 -0.2 -0.3 -0.3
2002 -0.2 -0.1 0.1 0.2 0.4 0.7 0.8 0.9 1.0 1.2 1.3 1.1
2003 0.9 0.6 0.4 0 -0.2 -0.1 0.1 0.2 0.3 0.4 0.4 0.4
2004 0.3 0.2 0.1 0.1 0.2 0.3 0.5 0.7 0.7 0.7 0.7 0.7
2005 0.6 0.6 0.5 0.5 0.4 0.2 0.1 0 0 -0.1 -0.4 -0.7
2006 -0.7 -0.6 -0.4 -0.2 0.0 0.1 0.2 0.3 0.5 0.8 0.9 1.0
2007 0.7 0.3 0 -0.1 -0.2 -0.2 -0.3 -0.6 -0.8 -1.1 -1.2 -1.3
2008 -1.4 -1.3 -1.1 -0.9 -0.7 -0.5 -0.3 -0.2 -0.2 -0.3 -0.5 -0.7
2009 -0.8 -0.7 -0.4 -0.1 0.2 0.4 0.5 0.6 0.7 1.0 1.2 1.3
2010 1.3 1.1 0.8 0.5 0 -0.4 -0.8 -1.1 -1.3 -1.4 -1.3 -1.4
2011 -1.3 -1.1 -0.8 -0.6 -0.3 -0.2 -0.3 -0.5 -0.7 -0.9 -0.9 -0.8
2012 -0.7 -0.6 -0.5 -0.4 -0.3 -0.1 0.1 0.3 0.4 0.4 0.2 -0.2
2013 -0.4 -0.5 -0.3 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.3
2014 -0.5 -0.6 -0.4 -0.2 0 0 0 0 0.2 0.4 0.6 0.6
2015 0.5 0.4 0.5 0.7 0.9 1.0 1.2 1.5 1.8 2.1 2.2 2.3
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[minor tick num=1,
xlabel=Degrees]
\addplot [black] table [x={year}, y={DJF}] {\data};
\addplot [black] table [x={year}, y={JFM}] {\data};
\addplot [black] table [x={year}, y={FMA}] {\data};
\addplot [black] table [x={year}, y={MAM}] {\data};
\addplot [black] table [x={year}, y={AMJ}] {\data};
\addplot [black] table [x={year}, y={MJJ}] {\data};
\addplot [black] table [x={year}, y={JJA}] {\data};
\addplot [black] table [x={year}, y={JAS}] {\data};
\addplot [black] table [x={year}, y={ASO}] {\data};
\addplot [black] table [x={year}, y={SON}] {\data};
\addplot [black] table [x={year}, y={OND}] {\data};
\addplot [black] table [x={year}, y={NDJ}] {\data};
\end{axis}
\end{tikzpicture}
\end{document}


To get either of the figures above it works for me (better if I can get both). Thanks.

• you could start with making a plot without the stuff you cannot do. So just provide an MWE without altering the colors of the \addplot. – Stefan Pinnow Mar 1 '16 at 18:00
• Done. @StefanPinnow – Cristóbal Mar 1 '16 at 18:55
• Then look up [fill between] on page 408 of the manual. – John Kormylo Mar 1 '16 at 22:19
• @Cristóbal, much of your code does not seem to be relevant to the question you're asking here. Please limit the example to only the code required for your issue to appear. You can have a look at this guide for how to prune your code for this purpose. This will make it much easier to read the question and so the chance increases, that people will read it up to the end and then provide an answer. – Stefan Pinnow Mar 3 '16 at 22:24
• The second example does not contribute anything new to the question and will only confuse people. You should remove it again. – Stefan Pinnow Mar 3 '16 at 22:26

Unfortunately you cannot simply use the fillbetween library of PGFPlots for the first example, because in your provided data there are values that equal 0, which is also the value where you want to "split" the data into an "upper" and a "lower" part that should be handled separatly. But that confuses the intersection library. To avoid this you either have to shift the horizontal line (where the data should be split) or you have to do some "manual" work to get the desired result.

The following code produces two examples. The first is a more "automated" approach which uses the small trick to shift the horizontal line a bit below zero to avoid the above mentioned problem, but of course when you have a close look you will see that the line is shifted. Here I show both, i.e. how you can draw the both parts in different colors and how to fill the areas in the corresponding parts. Both use the postaction and decoration features in combination with the soft clip feature.

The second approach results in the desired solution for the sake of a bit more "manual" work. There I use the intersection segments feature of the fillbetween library to manually set the "lower" and "upper" path of the plot respectively, compared to the horizontal line where the data are splitted.

For more details have a look at the comments in the code.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
pgfplots.fillbetween,
}
\pgfplotsset{
compat=1.11,
%
% define a style which can be used for both plots
my axis style/.style={
xmin=1950,
xmax=2015,
enlarge x limits={abs=5},
ymin=-2.5,
ymax=2.5,
minor tick num=1,
ylabel=$\Delta T$ $/$ K,
% remove the 1000 sep'
xticklabel style={
/pgf/number format/1000 sep={},
},
line join=bevel,
},
}
% missing value for the year 1978
% added a zero in the last column (NDJ)
\begin{filecontents*}{data.dat}
year DJF JFM FMA MAM AMJ MJJ JJA JAS ASO SON OND NDJ
1950 -1.4 -1.2 -1.1 -1.2 -1.1 -0.9 -0.6 -0.6 -0.5 -0.6 -0.7 -0.8
1951 -0.8 -0.6 -0.2 0.2 0.2 0.4 0.5 0.7 0.8 0.9 0.7 0.6
1952 0.5 0.4 0.4 0.4 0.4 0.2 0 0.1 0.2 0.2 0.2 0.3
1953 0.5 0.6 0.7 0.7 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.7
1954 0.7 0.4 0 -0.4 -0.5 -0.5 -0.5 -0.7 -0.7 -0.6 -0.5 -0.5
1955 -0.6 -0.6 -0.7 -0.7 -0.7 -0.6 -0.6 -0.6 -1.0 -1.4 -1.6 -1.4
1956 -0.9 -0.6 -0.6 -0.5 -0.5 -0.4 -0.5 -0.5 -0.4 -0.4 -0.5 -0.4
1957 -0.3 0 0.3 0.6 0.7 0.9 1.0 1.2 1.1 1.2 1.3 1.6
1958 1.7 1.5 1.2 0.8 0.7 0.6 0.5 0.4 0.4 0.5 0.6 0.6
1959 0.6 0.5 0.4 0.2 0.1 -0.2 -0.3 -0.3 -0.1 -0.1 -0.1 -0.1
1960 -0.1 -0.2 -0.1 0 -0.1 -0.2 0 0.1 0.2 0.1 0 0
1961 0 0 -0.1 0 0.1 0.2 0.1 -0.1 -0.3 -0.3 -0.2 -0.2
1962 -0.2 -0.2 -0.2 -0.3 -0.3 -0.2 -0.1 -0.2 -0.2 -0.3 -0.3 -0.4
1963 -0.4 -0.2 0.1  0.2 0.2 0.4 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1 -0.9 -0.7 -0.7 -0.6 -0.5 -0.6 -0.7 -0.8 -0.8
2001 -0.7 -0.6 -0.5 -0.3 -0.2 -0.1 0 -0.1 -0.1 -0.2 -0.3 -0.3
2002 -0.2 -0.1 0.1 0.2 0.4 0.7 0.8 0.9 1.0 1.2 1.3 1.1
2003 0.9 0.6 0.4 0 -0.2 -0.1 0.1 0.2 0.3 0.4 0.4 0.4
2004 0.3 0.2 0.1 0.1 0.2 0.3 0.5 0.7 0.7 0.7 0.7 0.7
2005 0.6 0.6 0.5 0.5 0.4 0.2 0.1 0 0 -0.1 -0.4 -0.7
2006 -0.7 -0.6 -0.4 -0.2 0.0 0.1 0.2 0.3 0.5 0.8 0.9 1.0
2007 0.7 0.3 0 -0.1 -0.2 -0.2 -0.3 -0.6 -0.8 -1.1 -1.2 -1.3
2008 -1.4 -1.3 -1.1 -0.9 -0.7 -0.5 -0.3 -0.2 -0.2 -0.3 -0.5 -0.7
2009 -0.8 -0.7 -0.4 -0.1 0.2 0.4 0.5 0.6 0.7 1.0 1.2 1.3
2010 1.3 1.1 0.8 0.5 0 -0.4 -0.8 -1.1 -1.3 -1.4 -1.3 -1.4
2011 -1.3 -1.1 -0.8 -0.6 -0.3 -0.2 -0.3 -0.5 -0.7 -0.9 -0.9 -0.8
2012 -0.7 -0.6 -0.5 -0.4 -0.3 -0.1 0.1 0.3 0.4 0.4 0.2 -0.2
2013 -0.4 -0.5 -0.3 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.3
2014 -0.5 -0.6 -0.4 -0.2 0 0 0 0 0.2 0.4 0.6 0.6
2015 0.5 0.4 0.5 0.7 0.9 1.0 1.2 1.5 1.8 2.1 2.2 2.3
\end{filecontents*}
\begin{document}
% first, more automated approach
% which gives almost the desired result
\begin{tikzpicture}
\begin{axis}[
my axis style,
]
% define a y value where to clip
% (this is needed because at exactly 0 you will get an
%  undesired result; give it a try to see what is happening)
\pgfmathsetmacro{\yclip}{-0.03}

% define a horizontal line where the values should be split
% into an upper and a lower part
\path [
draw=black,
name path=split path,
]
(\pgfkeysvalueof{/pgfplots/xmin},\yclip)
-- (\pgfkeysvalueof{/pgfplots/xmax},\yclip);

% draw the plot in blue
blue,
name path=curve,
% using postaction' and decorate' we draw the plot in red
% but clip it only to the "upper" part of using soft clip'
postaction={
decorate,
red,
thin,
},
decoration={
soft clip,
soft clip path={
(\pgfkeysvalueof{/pgfplots/xmin},\yclip)
rectangle
(\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax})
},
},
] table [x={year},y={DJF}] {\data};

% with the clipped curve' path we can now also fill the area
% (to do the same for the lower part you need to add another
%  \addplot' now clipping the "lower" part and then just add
%  another \addplot fill between')
\addplot [red!25]  fill between [of=split path and curve];
\end{axis}
\end{tikzpicture}

% second, more manual approach
% giving the wanted solution
\begin{tikzpicture}
\begin{axis}[
my axis style,
]
% what column should be printed
\newcommand*\ColName{DJF}

% define a horizontal line where the values should be split
% into an upper and a lower part
\path [
%                draw=black,
name path=origin,
]
(\pgfkeysvalueof{/pgfplots/xmin},0)
-- (\pgfkeysvalueof{/pgfplots/xmax},0);

% just plot one line
name path=curve,
] table [x={year},y=\ColName] {\data};

% compute + label the upper segment (but do not draw it):
\path [
name path=upper,
%                draw=red,
%                thick,
intersection segments={
of=origin and curve,
sequence=%
L{1} -- R{2} -- L{3} -- R{4} -- L{5}
-- L{6} -- R{7} -- L{8} -- R{9} -- L{10}
-- R{11} -- L{12} -- R{13} -- L{14} -- R{15}
-- R{16} -- L{17} -- R{18} -- L{19} -- R{20}
-- L{21} -- R{22} -- L{23} -- R{24} -- L{25}
-- R{26} -- L{27} -- R{28} -- L{29} -- R{30}
-- L{31} -- R{-1}
},
];

% compute + label the lower segment (but do not draw it):
\path [
name path=lower,
%                draw=blue,
%                thick,
intersection segments={
of=origin and curve,
sequence=%
R{1} -- L{2} -- R{3} -- L{4} -- R{5}
-- R{6} -- L{7} -- R{8} -- L{9} -- R{10}
-- L{11} -- R{12} -- L{13} -- R{14} -- L{15}
-- L{16} -- R{17} -- L{18} -- R{19} -- L{20}
-- R{21} -- L{22} -- R{23} -- L{24} -- R{25}
-- L{26} -- R{27} -- L{28} -- R{29} -- L{30}
-- R{31} -- L{-1}
},
];

% store the first and last value of the \data' table
\pgfplotstablegetelem{0}{year}\of{\data}
\pgfmathsetmacro{\FirstX}{\pgfplotsretval}
\pgfplotstablegetrowsof{\data}
\pgfmathsetmacro{\LastX}{\pgfplotsretval-1}
\pgfplotstablegetelem{\LastX}{year}\of{\data}
\pgfmathsetmacro{\LastX}{\pgfplotsretval}

% now plot the filled areas between the "origin" path and the
% computed "upper" and "lower" parts
\addplot [red!25]  fill between [
of=origin and upper,
% use another clip here to have a vertical start and end
% of the filled area
% (comment the next lines to see the difference)
soft clip={
domain=\FirstX:\LastX
},
];
\addplot [blue!25]  fill between [
of=origin and lower,
soft clip={
domain=\FirstX:\LastX
},
];
\end{axis}
\end{tikzpicture}

\end{document}


This is more a long comment than an answer itself.

First of all the your MWE is not working because the measurement NDJ in year 1978 is missing and the header of the table (first column) reads Year (capitol Y) while in the axis environment you call year. It would be easy to do if you re-arrange the data and include zeros between positive and negative values of anomalies. For example in 1992 you have 0.2 0 -0.1 and that's good whereas in 2002 there is -0.1 0.1 and you would need -0.1 0 0.1 with the extra care of adding the x coordinate accordingly. I would do that in Matlab.

Perhaps you can avoid this with some trick in pgfplotstable but I don't know how to do that.

Once you have the data rearranged with zeros you can use the option y filter/.expression={y>=0 ? nan : y} along with unbounded coords=jump (requires \pgfplotsset{compat=1.14} in the preamble). It means, check the y coordinate and if it is negative replace the value with nan, otherwise plot the point. These are options of the command addplot.

• Fixed data and capitol Y , I can't add an extra 0 though, in the example you mention. This is because in that case I'll be adding an extra data. The first 0 is, in fact a data for a single year. Thanks. – Cristóbal Mar 3 '16 at 12:56