# How to use multiple colorbars on one figure?

55 visualizzazioni (ultimi 30 giorni)
Leon il 3 Feb 2023
Commentato: Leon il 3 Feb 2023
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.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Benjamin Kraus il 3 Feb 2023
Modificato: Benjamin Kraus il 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 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Leon il 3 Feb 2023
They solve the problem for me beautifully.

Accedi per commentare.

### Categorie

Scopri di più su Colormaps in Help Center e File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by