How to use multiple colorbars on one figure?

52 次查看(过去 30 天)
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.

采纳的回答

Benjamin Kraus
Benjamin Kraus 2023-2-3
编辑:Benjamin Kraus 2023-2-3
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 个评论
Leon
Leon 2023-2-3
Thank you so much for your answer and the examples!
They solve the problem for me beautifully.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by