Matlab中,使用patch函数绘制面积堆叠图,也称为面积图。
主要思路是:
代码如下(含数据):
%%
clc;
clear;
%%
% 数据
x = [
1949,1950,1951,1952,1953,1954,1955,1956,1957,1958,1959,...
1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,...
1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,...
1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,...
1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,...
2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,...
2015,2016,2017,2018,2019,2020,2021
]';
y1 = [
379,126,360,667,311,185,371,170,275,338,473,294,365,...
365,382,468,488,384,521,402,433,572,564,654,434,501,...
291,715,711,366,790,659,476,540,750,505,741,1011,495,...
432,483,1025,964,1047,1317,659,832,824,1367,370,273,...
235,420,691,704,733,598,468,176,348,463,287,287,425,...
552,514,617,250,388,514,712,380,250
]' / 100.0; % 用整数除以100表示小数
y2 = [
2006,1786,2471,2215,1812,1960,1907,2445,2221,2812,2534,...
2601,2972,2626,3173,3303,3071,3200,3515,3583,2745,3630,...
4212,3934,3311,3839,3287,3720,2546,4123,3668,3165,3373,...
4120,3334,3903,4003,3570,3537,3315,3975,4464,3301,4662,...
3491,4571,3367,4310,4878,3812,3041,3478,2547,2583,3479,...
3361,3242,3229,2913,3409,3211,2879,2830,2634,3178,3312,...
3706,2990,2670,3455,3728,2883,2343
]' / 100.0; % 用整数除以100表示小数
y3 = [
1521,1979,1419,1459,1278,1342,1569,1623,1437,1617,1472,...
1654,1889,1534,1899,1812,1570,1927,2214,1836,1592,2105,...
2616,2072,1736,1975,1776,1818,1347,1977,1665,1611,1561,...
1240,1084,1975,1967,1544,1318,1313,2005,1680,1392,1745,...
1447,1943,1489,1762,1624,1478,1694,1619,1622,1517,1690,...
1666,1949,1367,1244,1624,1398,1300,1519,1688,1586,1519,...
1738,1838,1472,1788,1541,1666,1186
]' / 100.0; % 用整数除以100表示小数
y4 = [
522,647,529,421,412,375,604,419,507,435,550,530,589,...
596,575,555,682,654,802,734,603,803,918,755,506,593,...
645,648,534,688,541,577,639,417,305,666,619,474,604,...
638,556,615,483,675,426,693,623,611,703,458,617,640,...
701,584,642,658,892,425,525,523,393,422,530,811,447,...
656,611,684,677,934,638,703,640
]' / 100.0; % 用整数除以100表示小数
y5 = [
195,188,115,174,158,163,253,116,108,139,276,229,163,...
313,278,161,287,290,277,213,175,173,198,265,102,228,...
167,209,89,180,115,153,202,177,29,174,212,200,119,...
124,217,172,187,230,178,171,180,301,377,169,133,257,...
183,137,150,311,250,199,138,148,178,90,253,217,79,...
167,272,114,132,261,223,261,192
]' / 100.0; % 用整数除以100表示小数
%%
figure(1);
clrs = [
0 0.4470 0.7410;
0.8500 0.3250 0.0980;
0.9290 0.6940 0.1250;
0.4940 0.1840 0.5560;
0.4660 0.6740 0.1880;
0.3010 0.7450 0.9330;
0.6350 0.0780 0.1840;
]; % 颜色
gc = get(gca);
set(gcf, 'position', [0, 50, 900, 600]);
set(gca, 'FontName', 'Arial', 'FontSize', 24);
xlabel('X', 'FontSize', 24, 'FontName', 'Arial');
ylabel('Y', 'FontSize', 24, 'FontName', 'Arial');
hold on;
X = x;
Y = [y1, y2, y3, y4, y5];
Y1 = Y - Y;
for i = 1 : size(Y, 2)
for j = 1 : i
Y1(:, i) = Y1(:, i) + Y(:, j);
end
end
Y1 = [zeros(size(X)), Y1];
sp = [];
for i = 1 : size(Y1, 2) - 1
spi = patch([X; flipud(X)], [Y1(:, i); flipud(Y1(:, i + 1))], clrs(i, :), 'FaceA', .85, 'EdgeA', 0);
hold on;
sp = [sp; spi];
end
h = legend(sp, {'Legend1', 'Legend2', 'Legend3', 'Legend4', 'Legend5'}, 'Location', 'northwest');
set(h, 'FontName', 'Arial', 'FontSize', 22, 'FontWeight', 'normal');
h.ItemTokenSize = [50, 40];
hold on;
legend('boxoff');
xlim([min(X) max(X)]);
ylim([min(Y1(:)) max(Y1(:)) * 1.25]);
hold on;
rr = axis;
plot(rr(1 : 2), [rr(4), rr(4)], 'k-', [rr(2), rr(2)], rr(3 : 4), 'k-');
set(gca, 'looseInset', [0.12, 0.03, 0.03, 0.08]);
hold on;
set(gca, 'xtick', 1950 : 10 : 2020);