Displaying Colourbars in subplots
7 views (last 30 days)
Show older comments
Im trying to get a colour bar to show up for each one of these subplots, or one big one. I have tried using tiledlayout, the colorbar function and other online answers, but so far nothing is working. The strange thing is I can manually add a colorbar in the final figure window after the calculation, but they should be there automatically.
I haver attached my code below.
for ij = 1:length(times)
if ij == 1
indicol = find(hcontour1,1,'first');
[irow indcol] = find(hcontour1 == hcontour1(indicol));
indirow = find(hcontour1',1,'first');
[indrow icol] = find(hcontour1 == hcontour1(indirow));
subplot(4,1,1)
colorbar
yline(source_height*dy+dy,'--', 'Source')
hold on
quiver(Y(indrow:1:nrow,indcol:1:ncol),X(indrow:1:nrow,indcol:1:ncol),umyvec1(indrow:1:nrow,indcol:1:ncol), umxvec1(indrow:1:nrow,indcol:1:ncol),1)
hold on
contour(Y(indrow:nrow,indcol:ncol), X(indrow:nrow,indcol:ncol), hcontour1(indrow:nrow,indcol:ncol))
title(sprintf('Contour map of the fluid thickness at time = %2.0f%% of max time', times(ij)*100))
subtitle(sprintf('Incline angle of %2.0f degrees & an initial source fluid thickness of %1.0fm', angles(ii),thickness(jj)))
xlabel('Transverse Flow Dispersion (m)')
ylabel('Flow length(m)')
elseif ij == 2
indicol = find(hcontour2,1,'first');
[indrow indcol] = find(hcontour2 == hcontour2(indicol));
subplot(4,1,2)
yline(source_height*dy+dy,'--', 'Source')
hold on
quiver(Y(1:1:nrow,indcol:1:ncol),X(1:1:nrow,indcol:1:ncol),umyvec2(1:1:nrow,indcol:1:ncol), umxvec2(1:1:nrow,indcol:1:ncol),1)
hold on
contour(Y(:,indcol-10:ncol), X(:,indcol-10:ncol), hcontour2(:,indcol-10:ncol))
title(sprintf('Contour map of the fluid thickness at time = %2.0f%% of the max time', times(ij)*100))
subtitle(sprintf('Incline angle of %2.0f degrees & an initial source fluid thickness of %1.0fm', angles(ii),thickness(jj)))
xlabel('Transverse Flow Dispersion (m)')
ylabel('Flow length(m)')
colorbar
elseif ij == 3
indicol = find(hcontour3,1,'first');
[indrow indcol] = find(hcontour3 == hcontour3(indicol));
subplot(4,1,3)
yline(source_height*dy+dy,'--', 'Source')
hold on
quiver(Y(1:1:nrow,indcol:1:ncol),X(1:1:nrow,indcol:1:ncol),umyvec3(1:1:nrow,indcol:1:ncol), umxvec3(1:1:nrow,indcol:1:ncol),1)
hold on
contour(Y(:,indcol:ncol), X(:,indcol:ncol), hcontour3(:,indcol:ncol))
title(sprintf('Contour map of the fluid thickness at time = %2.0f%% of the max time', times(ij)*100))
subtitle(sprintf('Incline angle of %2.0f degrees & an initial source fluid thickness of %1.0fm', angles(ii),thickness(jj)))
xlabel('Transverse Flow Dispersion (m)')
ylabel('Flow length(m)')
colorbar
elseif ij == 4
indicol = find(hcontour4,1,'first');
[indrow indcol] = find(hcontour4 == hcontour4(indicol));
subplot(4,1,4)
colorbar
yline(source_height*dy+dy,'--', 'Source')
hold on
quiver(Y(1:1:nrow,indcol:1:ncol),X(1:1:nrow,indcol:1:ncol),umyvec4(1:1:nrow,indcol:1:ncol), umxvec4(1:1:nrow,indcol:1:ncol),1)
hold on
contour(Y(:,indcol:ncol), X(:,indcol:ncol), hcontour4(:,indcol:ncol))
title(sprintf('Contour map of the fluid thickness at time = %2.0f%% of the max time', times(ij)*100))
subtitle(sprintf('Inline angle of %2.0f degrees & an initial source fluid thickness of %1.0fm', angles(ii),thickness(jj)))
xlabel('Transverse Flow Dispersion (m)')
ylabel('Flow length(m)')
else
end
end
0 Comments
Answers (1)
Gautam
on 15 Feb 2024
Hello, Johnny
I tried simulating you code and I am able to see the colorbar for the individual subplots. The figure below shows that output that I get when I run my code
As you can see, the colorbar appears adjacent to all the constituent subplots
Below is the code I used to get the output
[X,Y,Z] = peaks;
for ij = 1:4
if ij ==1
a = 5*rand(1,20);
b = 5*rand(1,20);
c = 1:5;
d = 5*rand(1,20);
e = 5*rand(1,20);
subplot(4,1,1)
colorbar
yline(2.5, '--', 'Source')
hold on
quiver(a,b,d,e,1);
hold on
contour(X,Y,Z,20)
title(sprintf('Contour map of the fluid thickness at time = 20 of max time'))
subtitle(sprintf('Incline angle of 10 degrees & an initial source fluid thickness of 20'))
xlabel('Transverse Flow Dispersion (m)')
ylabel('Flow length(m)')
elseif ij ==2
a = 5*rand(1,20);
b = 5*rand(1,20);
c = 1:5;
d = 5*rand(1,20);
e = 5*rand(1,20);
subplot(4,1,2)
yline(2.5, '--', 'Source')
hold on
quiver(a,b,d,e,1);
hold on
contour(X,Y,Z,20)
title(sprintf('Contour map of the fluid thickness at time = 20 of max time'))
subtitle(sprintf('Incline angle of 10 degrees & an initial source fluid thickness of 20'))
xlabel('Transverse Flow Dispersion (m)')
ylabel('Flow length(m)')
colorbar
elseif ij ==3
a = 5*rand(1,20);
b = 5*rand(1,20);
c = 1:5;
d = 5*rand(1,20);
e = 5*rand(1,20);
subplot(4,1,3)
yline(2.5, '--', 'Source')
hold on
quiver(a,b,d,e,1);
hold on
contour(X,Y,Z,20)
title(sprintf('Contour map of the fluid thickness at time = 20 of max time'))
subtitle(sprintf('Incline angle of 10 degrees & an initial source fluid thickness of 20'))
xlabel('Transverse Flow Dispersion (m)')
ylabel('Flow length(m)')
colorbar
elseif ij ==4
a = 5*rand(1,20);
b = 5*rand(1,20);
c = 1:5;
d = 5*rand(1,20);
e = 5*rand(1,20);
subplot(4,1,4)
colorbar
yline(2.5, '--', 'Source')
hold on
quiver(a,b,d,e,1);
hold on
contour(X,Y,Z,20)
title(sprintf('Contour map of the fluid thickness at time = 20 of max time'))
subtitle(sprintf('Incline angle of 10 degrees & an initial source fluid thickness of 20'))
xlabel('Transverse Flow Dispersion (m)')
ylabel('Flow length(m)')
end
end
Please let me know if I am missing something or misinterpreting someting in your question
Here is a link to the MathWorks Documentation page for more information on the "colorbar" function
Thank You,
Gautam Murthy
0 Comments
See Also
Categories
Find more on Subplots 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!