Azzera filtri
Azzera filtri

How can I implement colormap in subplots?

1 visualizzazione (ultimi 30 giorni)
Martin Vallejos
Martin Vallejos il 1 Mag 2021
Commentato: Adam Danz il 3 Mag 2021
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 Commenti
Scott MacKenzie
Scott MacKenzie il 1 Mag 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:

Accedi per commentare.

Risposte (1)

Adam Danz
Adam Danz il 1 Mag 2021
Modificato: Adam Danz il 1 Mag 2021
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 Commenti
Martin Vallejos
Martin Vallejos il 2 Mag 2021
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 il 3 Mag 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.....

Accedi per commentare.

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by