How to use multiple colorbars on one figure?

53 views (last 30 days)
I have a figure with four subplots. The two subplots on the left are for tempeature, and the two subplots on the right side are for oxygen. They are two independent variables with different ranges. How do I achieve that?
Right now, I'm trying to plot the two subplots on the left side first and add their colorbar and then do the same thing for the two right side subplots. Unfortunately, the system will keep messing up the two subplots on the rights side and force them using the colormap info from the two right side subplots.
Many thanks.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 3 Feb 2023
Edited: Benjamin Kraus on 3 Feb 2023
There are two concepts that I think you may be confusing:
  1. The colormap which is a set of colors used to map from data to color. You can use the colormap command to set a colormap on a figure (and all the axes within the figure) or on an individual axes. By default it will apply to all the axes within the figure.
  2. The color limits. Each axes has a completely independent set of "color limits", which define the range of data that is mapped into the colormap. For example, if the CLim on the axes is [0 1], then data equal to (or less than) 0 will be mapped to the first color in the colormap, and data equal to (or greater than) 1 will be mapped to the last color in the colormap. The CLim is always independent for each axes (unless you explicitly link them using linkprop).
I suspect what you mean by "Unfortunately, the system will keep messing up the two subplots on the rights side and force them using the colormap info from the two right side subplots." is that you are accidentally changing the colormap on all the axes within the figure, instead of changing the color on individual axes.
For example:
tcl = tiledlayout(2,2);
topLeftAx = nexttile;
colorbar(topLeftAx)
% Note: I am passing a handle to the axes into the colormap command
% This will change the colormap on just the top left axes.
colormap(topLeftAx, 'spring')
topRightAx = nexttile;
colorbar(topRightAx)
% Note: I am passing a handle to the axes into the colormap command
% This will change the colormap on just the top right axes.
colormap(topRightAx, 'winter')
bottomLeftAx = nexttile;
colorbar(bottomLeftAx)
% Note: I am passing a handle to the axes into the colormap command
% This will change the colormap on just the bottom left axes.
colormap(bottomLeftAx, 'turbo')
bottomRightAx = nexttile;
colorbar(bottomRightAx)
% Note: I am passing a handle to the axes into the colormap command
% This will change the colormap on just the bottom right axes.
colormap(bottomRightAx, 'pink')
What I suspect you are doing is something closer to this:
figure
tcl = tiledlayout(2,2);
topLeftAx = nexttile;
colorbar(topLeftAx)
% Note: I am *not* passing a handle into the colormap command
% This changes the colormap on the one axes in the figure.
colormap('winter')
topRightAx = nexttile;
colorbar(topRightAx)
% Note: I am *not* passing a handle into the colormap command
% This changes the colormap on both axes currently in the figure.
colormap('spring')
bottomLeftAx = nexttile;
colorbar(bottomLeftAx)
% Note: I am *not* passing a handle into the colormap command
% This changes the colormap on all three axes in the figure.
colormap('pink')
bottomRightAx = nexttile;
colorbar(bottomRightAx)
% Note: I am *not* passing a handle into the colormap command
% This changes the colormap on all four axes.
colormap('turbo')
  1 Comment
Leon
Leon on 3 Feb 2023
Thank you so much for your answer and the examples!
They solve the problem for me beautifully.

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!