How do I display different colormap for subplots?

100 views (last 30 days)
HI all,
I am plotting 6 subplots in one figure in arrangement as 2 rows and 3 colums.
I want two colorbar with different colormap for every row.
How can I do it?
Thanks in advance.

Accepted Answer

Adam Danz
Adam Danz on 19 Sep 2022
  1. Use tiledlayout instead of subplot
  2. Assign the colormap to the axes using the axes handles
  3. Using tiledlayout, position the colorbars to the east|west|north|south
Full demo of these 3 steps
tcl = tiledlayout(2,3); % STEP 1
ax = gobjects(1,6);
for i = 1:6
ax(i) = nexttile();
peaks(15);
if i>3
colormap(ax(i),'hot') % STEP 2
else
colormap(ax(i),'cool')
end
end
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2) z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2) z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2) z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2) z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2) z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2)
% STEP 3
cb1 = colorbar(ax(3));
cb1.Layout.Tile = 'east';
cb1.Label.String = 'Vanilla Ice';
cb1 = colorbar(ax(6));
cb1.Layout.Tile = 'east';
cb1.Label.String = 'MC Hammer';
  3 Comments
Adam Danz
Adam Danz on 19 Sep 2022
Indeed!
Call the clim function for each axis specifying the same limits since you're using 1 colorbar scale for those axes.

Sign in to comment.

More Answers (1)

vamshi sai yele
vamshi sai yele on 19 Sep 2022
Hello,
I understood that you want to display different colors in a subplot.
I have tried it from my end and below is the solution for the same.
x=0:0.1:10;
y1 = cos(x)
y2 = sin(x)
subplot(2,2,1)
plot(x,y1,'Color','r') ;
colorbar
subplot(2,2,2)
plot(x,y2,'Color','r') ;
colorbar
subplot(2,2,3)
plot(x,y1,'Color','b') ;
colorbar
subplot(2,2,4)
plot(x,y2,'Color','b') ;
colorbar
In this example we have used 2x2 subplot and assigned red color to first row and blue color to second row. In this way we can set colors to individual plots as well.
We may also include different styled markers with different colors on the graph line. For such more options and better understanding of this concept, kindly refer to the following resources.
Hope you find it helpful!

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!