How to use multiple colorbars on one figure?

조회 수: 65 (최근 30일)
Leon
Leon 2023년 2월 3일
댓글: Leon 2023년 2월 3일
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