我在Matlab中画了一张如下图。我尝试了很多方法在图片末尾添加漂亮的颜色图例,每种颜色代表一个变量,例如“通货膨胀”、“利率”、“汇率”等。问题是我无法正确添加它们。我绘制图表的代码附在下面。
我使用命令
fill
画图,数据是一个矩阵(后面代码中没有提供,太大了)。有人会教我如何在图表底部绘制颜色图例吗?谢谢!
%% Graph 1
z1 = squeeze(z(i_var(1),:,:));
xmin = x(1);
xmax = x(end);
ix = z1 > 0;
ymax = max(sum(z1.*ix));
ix = z1 < 0;
ymin = min(sum(z1.*ix));
if ymax-ymin < 1e-6
end
figure('Name',endo_names(i_var(1),:));
subplot(2,1,1)
plot(x(2:end),z1(end,:),'k-','LineWidth',2)
hold on;
for i=1:gend
i_1 = i-1;
yp = 0;
ym = 0;
for k = 1:comp_nbr
zz = z1(k,i);
if zz > 0
fill([x(i) x(i) x(i+1) x(i+1)],[yp yp+zz yp+zz yp],k);
yp = yp+zz;
else
fill([x(i) x(i) x(i+1) x(i+1)],[ym ym+zz ym+zz ym],k);
ym = ym+zz;
end
hold on;
end
end
plot(x(2:end),z1(end,:),'k-','LineWidth',2),
set(gca,'xtick',[0 22 44 66 88 110]),
set(gca,'xticklabel',{'1985q1', '1990q3', '1996q1', '2001q3', '2007q3', '2013q1'}),title('Output gap')
axis([0 110 -3 3])
hold off;
%% Graph 2
z1 = squeeze(z(i_var(2),:,:));
xmin = x(1);
xmax = x(end);
ix = z1 > 0;
ymax = max(sum(z1.*ix));
ix = z1 < 0;
ymin = min(sum(z1.*ix));
if ymax-ymin < 1e-6
end
subplot(2,1,2)
plot(x(2:end),z1(end,:),'k-','LineWidth',2)
hold on;
for i=1:gend
i_1 = i-1;
yp = 0;
ym = 0;
for k = 1:comp_nbr
zz = z1(k,i);
if zz > 0
fill([x(i) x(i) x(i+1) x(i+1)],[yp yp+zz yp+zz yp],k);
yp = yp+zz;
else
fill([x(i) x(i) x(i+1) x(i+1)],[ym ym+zz ym+zz ym],k);
ym = ym+zz;
end
hold on;
end
end
plot(x(2:end),z1(end,:),'k-','LineWidth',2),set(gca,'xtick',[0 22 44 66 88 110])
set(gca,'xticklabel',{'1985q1', '1990q3', '1996q1', '2001q3', '2007q3', '2013q1'}), title('CPI inflation')
axis([0 110 -3 3])
hold off;