How to use multiple colorbars on one figure?

58 ビュー (過去 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 Center および File ExchangeColormaps についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by