Effacer les filtres
Effacer les filtres

multiple colorbars in one figure

58 vues (au cours des 30 derniers jours)
Kazemi Adachi
Kazemi Adachi le 26 Sep 2023
I would like to generate a colorbar for a plot which has two columns of colors. MWE of where I am now:
x = 0:10;
y1 = 1*x+(16:19)';
y2 = 2*x + (1:4)';
blue = [0 0.4470 0.7410];
orange = [0.8500 0.3250 0.0980];
colors = [1-(1-blue)*0.2;
1-(1-blue)*0.5;
1-(1-blue)*0.8;
blue;
1-(1-orange)*0.2;
1-(1-orange)*0.5;
1-(1-orange)*0.8;
orange];
figure;
colororder(colors)
hold on
plot(x,y1)
plot(x,y2)
colormap(colors)
colorbar
I would like the colorbar to look something more like this (edited in illustrator).
Is there a way to do this programmatically?
  1 commentaire
Dyuman Joshi
Dyuman Joshi le 26 Sep 2023
You have stated that this is a MWE of what you are attempting to do.
How many colorbars do you want to merge in this fashion for your original data? Based on the first sentence I would say 2, but I am not certain.

Connectez-vous pour commenter.

Réponse acceptée

Kazemi Adachi
Kazemi Adachi le 26 Sep 2023
The solution which best ended up working for me is similar to Walter's. But the easiest way I found to do this is to use another subplot with imagesc to get the proper formatting for a colormap with two columns.
x = 0:10;
y1 = 1*x+(16:19)';
y2 = 2*x + (1:4)';
blue = [0 0.4470 0.7410];
orange = [0.8500 0.3250 0.0980];
colors = [1-(1-blue)*0.2;
1-(1-blue)*0.5;
1-(1-blue)*0.8;
blue;
1-(1-orange)*0.2;
1-(1-orange)*0.5;
1-(1-orange)*0.8;
orange];
figure;
sbp1 = subplot(1,2,1);
colororder(colors)
hold on
plot(x,y1)
plot(x,y2)
im = zeros(4,2,3);
im(:,1,:) = colors(1:4,:);
im(:,2,:) = colors(5:8,:);
sbp2 = subplot(1,2,2);
imagesc(im)
axis off
sbp1.Position = [0.1 0.1 0.6 0.8];
sbp2.Position = [0.8 0.1 0.1 0.8];

Plus de réponses (1)

Walter Roberson
Walter Roberson le 26 Sep 2023
You can create multiple colorbar() using the 'Position' property. If colorbar() believes that a new colorbar overlaps an existing one then it will delete the existing one(s) considered to be overlapped.
The rule is not "only one colorbar per axes" -- you can, for example, colorbar('north') and colorbar('east') on the same axes.
Once you have the handle for a colorbar, you can change its Limits property. If you do that then the color patch and labels will update.
It is not possible to create more than one colormap in one axes. It is, however, possible to set the Colormap property of a colorbar to contain a colormap (N x 3 array of double). I'm not convinced it would be a good idea to do so, but it is possible

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by