How can I implement colormap in subplots?

1 次查看(过去 30 天)
Hi all, I am making a very simple figure with colormap, but, the commands I use cannot generate the uniformity of the subplots' legends. For example, I want the colormap to be jet in all the subplots, but I still can't get this result.
anyone have any idea how to get it?
clear all
clc
nametot;
output = readmatrix('graphs.xlsx','Sheet','outputimp');
cpi = readmatrix('graphs.xlsx','Sheet','cpiimp');
rin = readmatrix('graphs.xlsx','Sheet','rinimp');
tot = readmatrix('graphs.xlsx','Sheet','totimp');
name=graphsS16;
time=(1995:1/12:2021.2);
fillfcn = @(timerange,colour,lo,hi) fill([time(timerange) fliplr(time(timerange))], [ones(size(time(timerange)))*lo ones(size(time(timerange)))*hi], colour, 'FaceAlpha',0.2, 'LineWidth',0.2, 'EdgeColor', 'none'); % Function Ot Create ?fill? Regions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Build colormap and shuffle
cmap = colormap(jet(size(output,2)));
cmap = cmap(randperm(length(cmap)),:)
%Set colororder and plot
ax = axes('colororder',cmap);
hold on
figure(1)
subplot(2,2,1)
plot(time, output, 'LineWidth',2);
xlim([1994.9, 2021.3])
ylim([-.4, 0.4])
hold on
fillfcn((24:73), 'k', -.4, 0.4)
fillfcn((163:178), 'k', -.4, 0.4)
fillfcn((237:254), 'k', -.4, 0.4)
fillfcn((295:310), 'k', -.4, 0.4)
title('Output Gap','Fontsize',13)
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11)
grid minor
subplot(2,2,2)
plot(time, cpi, 'LineWidth',2);
xlim([1994.9, 2021.3])
ylim([-1, 1.5])
hold on
fillfcn((24:73), 'k', -1, 1.5)
fillfcn((163:178), 'k', -1, 1.5)
fillfcn((237:254), 'k', -1, 1.5)
fillfcn((295:310), 'k', -1, 1.5)
title('Rate of Inflation','Fontsize',13)
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11)
grid minor
subplot(2,2,3)
plot(time, rin, 'LineWidth',2)
xlim([1994.9, 2021.3])
ylim([-1.5, 2])
hold on
fillfcn((24:73), 'k', -1.5, 2)
fillfcn((163:178), 'k', -1.5, 2)
fillfcn((237:254), 'k', -1.5, 2)
fillfcn((295:310), 'k', -1.5, 2)
title('International Reserves excluding Gold','Fontsize',13)
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11)
grid minor
subplot(2,2,4)
plot(time, tot, 'LineWidth',2);
xlim([1994.9, 2021.3])
ylim([-1.5, 1.5])
hold on
fillfcn((24:73), 'k', -1.5, 1.5)
fillfcn((163:178), 'k', -1.5, 1.5)
fillfcn((237:254), 'k', -1.5, 1.5)
fillfcn((295:310), 'k', -1.5, 1.5)
title('Term of Trade','Fontsize',12)
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11)
grid minor
legend(name, "FontName", "Times New Roman", "FontSize", 12, 'Position',[-0.02 0 0.15 0.99]);
hold off
  2 个评论
Scott MacKenzie
Scott MacKenzie 2021-5-1
I can't excute your code because you haven't uploaded the data. Perhaps all you need to do is move your colormap code into the subplot section for each plot:

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2021-5-1
编辑:Adam Danz 2021-5-1
We can't run your code even with the attached data.mat file because it does not contain all of the variables or functions in your code (e.g. nametot is missing and possibly others).
Nevertheless the problem is likely due to how you're assigning the colormap to the axes.
The problem
You are correctly assigning the colormap to the ColorOrder property of axes in this line....
ax = axes('colororder',cmap);
However, those axes are destroyed and replaced by new axes the first time you call subplot for the same figure,
subplot(2,2,1)
The Solution
Assign the color map to every subplot axes using the template below and eliminate the ax=axes(...) lines.
ax = subplot(2,2,n);
ax.ColorOrder = cmap;
  9 个评论
Martin Vallejos
Martin Vallejos 2021-5-2
problem solved :)
figure()
hold all
N = size(output,2);
colororder(hsv(N));
subplot(2,2,1)
as=plot(time,output ,'-', 'LineWidth',2);
xlim([1994.9, 2021.3]);
ylim([-.4, 0.4]);
hold on
fillfcn((24:73), 'k', -.4, 0.4);
fillfcn((163:178), 'k', -.4, 0.4);
fillfcn((237:254), 'k', -.4, 0.4);
fillfcn((295:310), 'k', -.4, 0.4);
hold off
title('Output Gap','Fontsize',13)
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11)
grid minor
subplot(2,2,2);
N = size(cpi,2);
colororder(hsv(N));
as=plot(time,cpi ,'-', 'LineWidth',2);
xlim([1994.9, 2021.3]);
ylim([-1, 1.5]);
hold on
fillfcn((24:73), 'k', -1, 1.5);
fillfcn((163:178), 'k', -1, 1.5);
fillfcn((237:254), 'k', -1, 1.5);
fillfcn((295:310), 'k', -1, 1.5);
title('Rate of Inflation','Fontsize',13);
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11);
grid minor
subplot(2,2,3);
N = size(rin,2);
colororder(hsv(N));
as=plot(time,rin,'-', 'LineWidth',2);
xlim([1994.9, 2021.3]);
ylim([-1.5, 2]);
hold on
fillfcn((24:73), 'k', -1.5, 2);
fillfcn((163:178), 'k', -1.5, 2);
fillfcn((237:254), 'k', -1.5, 2);
fillfcn((295:310), 'k', -1.5, 2);
title('International Reserves excluding Gold','Fontsize',13);
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11);
grid minor
subplot(2,2,4);
N = size(tot,2);
colororder(hsv(N));
as=plot(time,tot,'-', 'LineWidth',2);
xlim([1994.9, 2021.3]);
ylim([-1.5, 1.5]);
hold on
fillfcn((24:73), 'k', -1.5, 1.5);
fillfcn((163:178), 'k', -1.5, 1.5);
fillfcn((237:254), 'k', -1.5, 1.5);
fillfcn((295:310), 'k', -1.5, 1.5);
title('Term of Trade','Fontsize',12);
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11);
grid minor
legend(as, name, "FontName", "Times New Roman", "FontSize", 12, 'Position',[-0.02 0 0.15 0.99]);
hold off
Adam Danz
Adam Danz 2021-5-3
You're using the colororder function instead of setting colororder directly. But I don't think you understand why your initial code didn't work. As I explained in my answer, 1) when you call subplot, it removed the axes that you already created and 2) you have to set the ColorOrder property of each subplot.
Here's what you should have done to implement the solution in my answer (see the <------ arrows).
clear all
clc
nametot;
output = readmatrix('graphs.xlsx','Sheet','outputimp');
cpi = readmatrix('graphs.xlsx','Sheet','cpiimp');
rin = readmatrix('graphs.xlsx','Sheet','rinimp');
tot = readmatrix('graphs.xlsx','Sheet','totimp');
name=graphsS16;
time=(1995:1/12:2021.2);
fillfcn = @(timerange,colour,lo,hi) fill([time(timerange) fliplr(time(timerange))], [ones(size(time(timerange)))*lo ones(size(time(timerange)))*hi], colour, 'FaceAlpha',0.2, 'LineWidth',0.2, 'EdgeColor', 'none'); % Function Ot Create ?fill? Regions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Build colormap and shuffle
cmap = colormap(jet(size(output,2)));
cmap = cmap(randperm(length(cmap)),:)
%Set colororder and plot
% ax = axes('colororder',cmap); % <---------- REMOVE, it's meaningless
% hold on % <---------- REMOVE, it's meaningless
figure(1)
ax = subplot(2,2,1); % <---------- add output
ax.ColorOrder = cmap; % <---------- Set colormap
plot(time, output, 'LineWidth',2);
xlim([1994.9, 2021.3])
ylim([-.4, 0.4])
hold on
fillfcn((24:73), 'k', -.4, 0.4)
fillfcn((163:178), 'k', -.4, 0.4)
fillfcn((237:254), 'k', -.4, 0.4)
fillfcn((295:310), 'k', -.4, 0.4)
title('Output Gap','Fontsize',13)
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11)
grid minor
ax = subplot(2,2,2); % <-------- REPEATE
ax.ColorOrder = cmap; % <-------- REPEATE
plot(time, cpi, 'LineWidth',2);
xlim([1994.9, 2021.3])
ylim([-1, 1.5])
hold on
fillfcn((24:73), 'k', -1, 1.5)
fillfcn((163:178), 'k', -1, 1.5)
fillfcn((237:254), 'k', -1, 1.5)
fillfcn((295:310), 'k', -1, 1.5)
title('Rate of Inflation','Fontsize',13)
xlabel('Time: 1995:m1 - 2021:m3','Fontsize',11)
grid minor
% etc.....

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by