How can I implement colormap in subplots?

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:
Martin Vallejos
Martin Vallejos 2021 年 5 月 1 日
I attach the data

サインインしてコメントする。

回答 (1 件)

Adam Danz
Adam Danz 2021 年 5 月 1 日
編集済み: Adam Danz 2021 年 5 月 1 日

0 投票

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 月 1 日
ohh yeah I didn't attach all variables, so sorry, but I tried with all the options and the result does not come out as I expect. I think I am wrong in some detail :(
I attach again the dataset.
Adam Danz
Adam Danz 2021 年 5 月 1 日
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
Martin Vallejos 2021 年 5 月 1 日
nametot={"United State", "China", "India","Japan", "South Korea", "Brazil", "Germany", "United Kingdom", "Saudi Arabia", "Russia", "Canada" , "Mexico", "Norway","Indonesia","Malaysia","Ecuador"}
Martin Vallejos
Martin Vallejos 2021 年 5 月 1 日
nametot={"United State", "China", "India","Japan", "South Korea", "Brazil", "Germany", "United Kingdom", "Saudi Arabia", "Russia", "Canada" , "Mexico", "Norway","Indonesia","Malaysia","Ecuador"}
Adam Danz
Adam Danz 2021 年 5 月 1 日
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
Martin Vallejos 2021 年 5 月 1 日
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
load data
% 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()
ax=subplot(2,2,1)
ax = axes('colororder',cmap);
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)
ax = axes('colororder',cmap);
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
ax=subplot(2,2,3)
ax = axes('colororder',cmap);
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
ax=subplot(2,2,4)
ax = axes('colororder',cmap);
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
it does not work :(
Adam Danz
Adam Danz 2021 年 5 月 1 日
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
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.....

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeColor and Styling についてさらに検索

タグ

質問済み:

2021 年 5 月 1 日

コメント済み:

2021 年 5 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by