How can I implement colormap in subplots?
Show older comments
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 Comments
Scott MacKenzie
on 1 May 2021
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:

Martin Vallejos
on 1 May 2021
Answers (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 Comments
Martin Vallejos
on 1 May 2021
Adam Danz
on 1 May 2021
When I load that file a warning appears,
Warning: Could not find appropriate function on path loading function handle
/Users/macbookpro/Desktop/HSE/TESIS/figure2.m>@(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')
and the same variable nametot is missing. We only need the variables and any personal functions used in the code.
Actually, we don't really need anything unless the solution I mentioned isn't working for you. What does it mean "I tried with all the options"? Can you provide your updated code with the changes I suggested?
Martin Vallejos
on 1 May 2021
Martin Vallejos
on 1 May 2021
Adam Danz
on 1 May 2021
We also don't have graphs.xlsx.
But first, please show us the updated code that implements my solution and descrive the remaining problem if there is one.
There's no need to run your code before confirming that you've implemented the solution correctly and understanding any remaining problems.
Martin Vallejos
on 1 May 2021
Adam Danz
on 1 May 2021
No, this isn't what I suggested in my answer,
% your code (incorrect)
ax=subplot(2,2,1)
ax = axes('colororder',cmap);
Please look at the answer again.
The solution requires to eliminate the ax=axes(....) lines and to use the subplot output handles.
Martin Vallejos
on 2 May 2021
Adam Danz
on 3 May 2021
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.....
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!