Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate area plot for time series in MATLAB

Tags:

matlab

I'm trying to generate this figure shown below:

figure
(source: noaa.gov)

The data that I am using to try and generate this figure can be found here data.

This is a subset of the data that I extracted:

d = [1950    1   0.56
1950    2   0.01
1950    3  -0.78
1950    4   0.65
1950    5  -0.50
1950    6   0.25
1950    7  -1.23
1950    8  -0.19
1950    9   0.39
1950   10   1.43
1950   11  -1.46
1950   12  -1.03
1951    1  -0.42
1951    2   0.35
1951    3  -1.47
1951    4  -0.38
1951    5  -0.50
1951    6  -1.35
1951    7   1.39
1951    8  -0.41
1951    9  -1.18
1951   10   2.54
1951   11  -0.54
1951   12   1.13
1952    1   0.57
1952    2  -1.38
1952    3  -1.97
1952    4   0.95
1952    5  -0.99
1952    6  -0.10
1952    7  -0.06
1952    8  -0.49
1952    9  -0.38
1952   10  -0.28
1952   11  -1.32
1952   12  -0.49
1953    1  -0.12
1953    2  -1.00
1953    3  -0.45
1953    4  -1.96
1953    5  -0.56
1953    6   1.41
1953    7   0.43
1953    8  -1.04
1953    9  -0.19
1953   10   1.95
1953   11   0.96
1953   12  -0.52
1954    1  -0.08
1954    2   0.40
1954    3  -1.27
1954    4   1.31
1954    5  -0.03
1954    6   0.06
1954    7  -0.57
1954    8  -2.57
1954    9  -0.28
1954   10   1.16
1954   11   0.29
1954   12   0.55
1955    1  -2.65
1955    2  -1.71
1955    3  -0.96
1955    4  -0.60
1955    5  -0.26
1955    6  -0.80
1955    7   1.78
1955    8   1.25
1955    9   0.46
1955   10  -1.09
1955   11  -1.49
1955   12   0.07
1956    1  -0.76
1956    2  -1.71
1956    3  -0.46
1956    4  -1.30
1956    5   2.10
1956    6   0.41
1956    7  -0.72
1956    8  -1.89
1956    9   0.38
1956   10   1.47
1956   11   0.40
1956   12   0.00
1957    1   0.71
1957    2  -0.32
1957    3  -1.73
1957    4   0.39
1957    5  -0.68
1957    6  -0.42
1957    7  -1.16
1957    8  -0.83
1957    9  -1.47
1957   10   1.95
1957   11   0.63
1957   12   0.02
1958    1  -1.14
1958    2  -1.64
1958    3  -2.46
1958    4   0.26
1958    5  -0.17
1958    6  -1.08
1958    7  -1.69
1958    8  -2.13
1958    9   0.08
1958   10   0.68
1958   11   1.59
1958   12  -0.74
1959    1  -1.52
1959    2   0.33
1959    3  -0.56
1959    4   0.25
1959    5   0.41
1959    6   0.71
1959    7   0.77
1959    8  -0.05
1959    9   1.00
1959   10   1.48
1959   11   0.30
1959   12   0.32
1960    1  -2.01
1960    2  -2.59
1960    3  -0.93
1960    4   1.33
1960    5   0.47
1960    6   0.10
1960    7   0.38
1960    8  -1.93
1960    9   0.53
1960   10  -1.37
1960   11  -0.67
1960   12  -0.03
1961    1  -0.03
1961    2   0.07
1961    3   0.17
1961    4  -1.83
1961    5  -0.28
1961    6   1.17
1961    7  -0.36
1961    8   1.03
1961    9   1.36
1961   10   1.07
1961   11  -0.79
1961   12  -1.46
1962    1   0.20
1962    2   0.18
1962    3  -2.99
1962    4   0.93
1962    5  -0.04
1962    6   0.47
1962    7  -2.43
1962    8   0.05
1962    9  -0.21
1962   10   0.96
1962   11  -0.38
1962   12  -1.31
1963    1  -2.98
1963    2  -1.53
1963    3  -0.85
1963    4  -1.61
1963    5   2.05
1963    6  -0.13
1963    7  -0.74
1963    8  -0.95
1963    9   1.89
1963   10   1.53
1963   11  -1.47
1963   12  -1.87
1964    1  -1.62
1964    2  -2.06
1964    3  -1.66
1964    4   0.25
1964    5   0.53
1964    6   1.61
1964    7   1.92
1964    8  -2.40
1964    9   0.34
1964   10   1.32
1964   11  -0.14
1964   12  -0.23
1965    1  -0.65
1965    2  -2.20
1965    3  -1.99
1965    4   0.64
1965    5  -0.52
1965    6   0.60
1965    7   0.35
1965    8   0.45
1965    9   0.51
1965   10   0.92
1965   11  -1.88
1965   12   1.18
1966    1  -2.54
1966    2  -2.02
1966    3   0.18
1966    4  -0.96
1966    5   0.25
1966    6   1.37
1966    7   0.35
1966    8  -2.39
1966    9  -0.29
1966   10  -0.23
1966   11  -0.18
1966   12   0.58];

I've tried the code below to generate the above plot:

time = datenum(d(:,1),d(:,2),repmat(15,size(d,1),1));
nao = d(:,3);

figure(1);
ax1 = subplot(211);
area(time(nao < 0),nao(nao < 0),'FaceColor',[86 180 233]/255,'EdgeColor','none');
hold on;
area(time(nao > 0),nao(nao > 0),'FaceColor','r','EdgeColor','none');
datetick('x','yyyy','keeplimits');

ax2 = subplot(212);
bar(time(nao < 0),nao(nao < 0),'FaceColor',[86 180 233]/255,'EdgeColor','none');
hold on;
bar(time(nao > 0),nao(nao > 0),'FaceColor','r','EdgeColor','none');
datetick('x','yyyy','keeplimits');

linkaxes([ax1 ax2], 'x');

However, the output is not the same as that shown in the original figure, granted that they have a three month moving average. Ideally, I would have something that looked half like each of the plots I generated. That is, an area plot but with only a red or blue color at any given time. The area function seems to show these to happen at the same time, which is not correct. Any advice on how to generate the attached figure would be appreciated.

like image 756
Emma Tebbs Avatar asked Oct 30 '22 14:10

Emma Tebbs


1 Answers

Using areashade from MATLAB FileExchange, the following should produce what you need:

figure(2);
areashade(time, nao, 0, [86 180 233]/255, 'h')
areashade(time, nao, 0,'r','l')
datetick('x','yyyy','keeplimits');
like image 157
zeeMonkeez Avatar answered Nov 08 '22 05:11

zeeMonkeez