multiple colorbars in one figure
58 views (last 30 days)
Show older comments
Kazemi Adachi
on 26 Sep 2023
Answered: Kazemi Adachi
on 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 Comment
Dyuman Joshi
on 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.
Accepted Answer
More Answers (1)
Walter Roberson
on 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
0 Comments
See Also
Categories
Find more on Colormaps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!