multiple colorbars in one figure

59 次查看(过去 30 天)
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-9-26
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.

请先登录,再进行评论。

采纳的回答

Kazemi Adachi
Kazemi Adachi 2023-9-26
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];

更多回答(1 个)

Walter Roberson
Walter Roberson 2023-9-26
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

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by