Colour bar isn't correct for subplot contour

1 view (last 30 days)
Hello,
Been looking through the forums for help with this, and found some solutions but did not fix my issue.
(Code attached at bottom). The z-values on the left plot do not quite match up with my globally defined colourbar.
I know contours, in this instance, work with discrete points, so unsure if the issue is with that.
I could plot two separate bars which address the issue (see commented out) but I would rather just have the one bar.
Is it possible I need to simulate more combinations such that the plot is more 'exact'?
Thanks!
%The action of drug on logsitic growth model - sensitivity analysis%
%Preamble
clc
clear all
clear workspace
%Parameters - Fixed:
%All the different initial conditions
MPC1 = [10000 000];
MPC2 = [7500 2500];
MPC3 = [5000 5000];
MPC4 = [2500 7500];
MPC5 = [000 10000];
g1 = 1.0392; %Phase 1 transiton
g2 = 1.0392; %Phase 2 transition
x0 = 10000; %Initial cell count
K = 100000; %Maximal cell count
time = linspace(0,15,1000); %time of simulation
%Parameters that vary:
cd = 1e-3;
kmax = [1 0.95 0.9 0.85 0.8 0.75 0.7 0.65 0.6 0.55 0.5];
k50 = [1e-3 7.5e-4 2.5e-4 1e-4 5e-5 1e-5 5e-6 1e-6 7.5e-7 5e-7 1e-7];
%Initial condition 1
for ii = 1:length(kmax)
for kk = 1:length(k50)
M = (cd*kmax(ii))/(cd+k50(kk));
ode = @(t,x) [2*g2*x(2)*(1 - ((x(1)+x(2))/K)) - g1*x(1)*(1-M);...
-g2*x(2)*(1 - ((x(1)+x(2))/K)) + g1*x(1)*(1-M)];
[t, x(:,:,ii,kk)] = ode45(ode, time, MPC1);
end
end
%Initial condition 5
for ii = 1:length(kmax)
for kk = 1:length(k50)
M = (cd*kmax(ii))/(cd+k50(kk));
ode = @(t,x) [2*g2*x(2)*(1 - ((x(1)+x(2))/K)) - g1*x(1)*(1-M);...
-g2*x(2)*(1 - ((x(1)+x(2))/K)) + g1*x(1)*(1-M)];
[t, x5(:,:,ii,kk)] = ode45(ode, time, MPC5);
end
end
%Data Extraction:
%This informs us how long our data set is
CellS = x(:,:);
CellS5 = x5(:,:);
%Loop length
LL = length(CellS(1,:))/2;
%Adding the first and second phase numbers together
counter = 0; %Initialise counter for initial grouping
for ii = 1:LL
T_1(:,ii) = CellS(:,ii+counter) + CellS(:,(ii+1)+counter);
T_5(:,ii) = CellS5(:,ii+counter) + CellS5(:,(ii+1)+counter);
%Skips to next group
counter = counter + 1;
end
%Specific times - Halfway point
for ii = 1:LL
T1(:,ii) = T_1(length(t)/2,ii);
T5(:,ii) = T_5(length(t)/2,ii);
end
x_contour = k50';
y_contour = kmax';
[Y,X] = meshgrid(y_contour,x_contour);
%Collects data by k50 - each entry is the different kmax values for a given
%k50
%MPC1
Z1 = [T1(:,1:11);T1(:,12:22);T1(:,23:33);T1(:,34:44);...
T1(:,45:55);T1(:,56:66);T1(:,67:77);T1(:,78:88);...
T1(:,89:99);T1(:,100:110);T1(:,111:121)];
%MPC5
Z5 = [T5(:,1:11);T5(:,12:22);T5(:,23:33);T5(:,34:44);...
T5(:,45:55);T5(:,56:66);T5(:,67:77);T5(:,78:88);...
T5(:,89:99);T5(:,100:110);T5(:,111:121)];
fig=figure(1)
%Tile 1
subplot(1,2,1)
contourf(Y,X,Z1,'Linewidth',2) %G1D1
colormap(parula)
% cb = colorbar;
% cb.LineWidth = 1.5;
set(gca,'YScale','log')
xlabel('k_{max}'), ylabel('k_{50} [M]')
xticks(0.5:0.1:1)
get(gca,'fontname') % shows you what you are using.
set(gca,'linewidth', 2,'fontsize',24,'fontname','Helectiva') % Sets the width of the axis lines, font size, font
set(gca,'TickDir','out')
set(gca,'YScale','log')
%Tile 2
subplot(1,2,2)
contourf(Y,X,Z5,'Linewidth',2)
% cb = colorbar;
% cb.LineWidth = 1.5;
set(gca,'YScale','log')
xlabel('k_{max}'), ylabel('k_{50} [M]')
xticks(0.5:0.1:1)
get(gca,'fontname') % shows you what you are using.
set(gca,'linewidth', 2,'fontsize',24,'fontname','Helectiva') % Sets the width of the axis lines, font size, font
set(gca,'TickDir','out')
set(gca,'YScale','log')
h = axes(fig,'visible','off');
c = colorbar(h,'Position',[0.93 0.168 0.022 0.7]); % attach colorbar to h
c.LineWidth = 1.5;
c.FontSize = 24;
c.Label.String = 'Cell Number';
bottom = min(min(min(Z1)),min(min(Z5)));
top = max(max(max(Z1)),max(max(Z5)));
caxis(h,[bottom, top]);
  1 Comment
Alistair McQueen
Alistair McQueen on 23 Feb 2022
To emphasise this, plotting with two separate colourbars gives the results attached.
This is more prominent when changing the cd value to 1e-5.

Sign in to comment.

Accepted Answer

Simon Chan
Simon Chan on 23 Feb 2022
Below is only a workaround to use each colorbar for each subplot and finally hidden them.
%Preamble
clc
clear all
clear workspace
%Parameters - Fixed:
%All the different initial conditions
MPC1 = [10000 000];
MPC2 = [7500 2500];
MPC3 = [5000 5000];
MPC4 = [2500 7500];
MPC5 = [000 10000];
g1 = 1.0392; %Phase 1 transiton
g2 = 1.0392; %Phase 2 transition
x0 = 10000; %Initial cell count
K = 100000; %Maximal cell count
time = linspace(0,15,1000); %time of simulation
%Parameters that vary:
cd = 1e-3;
kmax = [1 0.95 0.9 0.85 0.8 0.75 0.7 0.65 0.6 0.55 0.5];
k50 = [1e-3 7.5e-4 2.5e-4 1e-4 5e-5 1e-5 5e-6 1e-6 7.5e-7 5e-7 1e-7];
%Initial condition 1
for ii = 1:length(kmax)
for kk = 1:length(k50)
M = (cd*kmax(ii))/(cd+k50(kk));
ode = @(t,x) [2*g2*x(2)*(1 - ((x(1)+x(2))/K)) - g1*x(1)*(1-M);...
-g2*x(2)*(1 - ((x(1)+x(2))/K)) + g1*x(1)*(1-M)];
[t, x(:,:,ii,kk)] = ode45(ode, time, MPC1);
end
end
%Initial condition 5
for ii = 1:length(kmax)
for kk = 1:length(k50)
M = (cd*kmax(ii))/(cd+k50(kk));
ode = @(t,x) [2*g2*x(2)*(1 - ((x(1)+x(2))/K)) - g1*x(1)*(1-M);...
-g2*x(2)*(1 - ((x(1)+x(2))/K)) + g1*x(1)*(1-M)];
[t, x5(:,:,ii,kk)] = ode45(ode, time, MPC5);
end
end
%Data Extraction:
%This informs us how long our data set is
CellS = x(:,:);
CellS5 = x5(:,:);
%Loop length
LL = length(CellS(1,:))/2;
%Adding the first and second phase numbers together
counter = 0; %Initialise counter for initial grouping
for ii = 1:LL
T_1(:,ii) = CellS(:,ii+counter) + CellS(:,(ii+1)+counter);
T_5(:,ii) = CellS5(:,ii+counter) + CellS5(:,(ii+1)+counter);
%Skips to next group
counter = counter + 1;
end
%Specific times - Halfway point
for ii = 1:LL
T1(:,ii) = T_1(length(t)/2,ii);
T5(:,ii) = T_5(length(t)/2,ii);
end
x_contour = k50';
y_contour = kmax';
[Y,X] = meshgrid(y_contour,x_contour);
%Collects data by k50 - each entry is the different kmax values for a given
%k50
%MPC1
Z1 = [T1(:,1:11);T1(:,12:22);T1(:,23:33);T1(:,34:44);...
T1(:,45:55);T1(:,56:66);T1(:,67:77);T1(:,78:88);...
T1(:,89:99);T1(:,100:110);T1(:,111:121)];
%MPC5
Z5 = [T5(:,1:11);T5(:,12:22);T5(:,23:33);T5(:,34:44);...
T5(:,45:55);T5(:,56:66);T5(:,67:77);T5(:,78:88);...
T5(:,89:99);T5(:,100:110);T5(:,111:121)];
fig=figure(1);
%Tile 1
subplot(1,2,1)
contourf(Y,X,Z1,'Linewidth',2); %G1D1
colormap(parula)
cb1 = colorbar;
cb1.LineWidth = 1.5;
ax1=gca;
set(gca,'YScale','log')
xlabel('k_{max}'), ylabel('k_{50} [M]');
xticks(0.5:0.1:1);
get(gca,'fontname'); % shows you what you are using.
set(gca,'linewidth', 2,'fontsize',24,'fontname','Helectiva'); % Sets the width of the axis lines, font size, font
set(gca,'TickDir','out');
set(gca,'YScale','log');
%Tile 2
subplot(1,2,2)
contourf(Y,X,Z5,'Linewidth',2);
cb2 = colorbar;
cb2.LineWidth = 1.5;
ax2=gca;
set(gca,'YScale','log');
xlabel('k_{max}'), ylabel('k_{50} [M]');
xticks(0.5:0.1:1);
get(gca,'fontname') % shows you what you are using.
set(gca,'linewidth', 2,'fontsize',24,'fontname','Helectiva'); % Sets the width of the axis lines, font size, font
set(gca,'TickDir','out')
set(gca,'YScale','log')
h = axes(fig,'visible','off');
bottom = min(min(min(Z1)),min(min(Z5)));
top = max(max(max(Z1)),max(max(Z5)));
caxis(ax1,[bottom, top]); % Set the same limit to first subplot
caxis(ax2,[bottom, top]); % Set the same limit to second subplots
cb1.Position = [0 0 0 0]; % Reduce the position for cb1
cb2.Position = [0 0 0 0]; % Reduce the position for cb2
c = colorbar(h,'Position',[0.93 0.168 0.022 0.7]); % attach colorbar to h
c.LineWidth = 1.5;
c.FontSize = 24;
c.Label.String = 'Cell Number';
caxis(cb1.Limits); % Set the new colorbar limits
  1 Comment
Alistair McQueen
Alistair McQueen on 23 Feb 2022
Okay, although a workaround, I'm happy that it works. Previous iteration was really annoying me.
I'll accept the answer, but if anyone has more optimised code feel free to share, I am okay with this version!
Thank you :)

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!