필터 지우기
필터 지우기

multiple colorbars in one figure

조회 수: 59 (최근 30일)
Kazemi Adachi
Kazemi Adachi 2023년 9월 26일
답변: Kazemi Adachi 2023년 9월 26일
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