How to adjust the view of colorbar to be the same as contourf

1 view (last 30 days)
Hello everyone, I tried many ways to adjust the view of the colorbar to the same view of the fill image, but failed.
%%
x = 100*rand(100,100);
contourf(x,'Linestyle','none');
set(gca,'view',([5,30]));
axis([0 100 0 100 0 1]);
c = colorbar;
%%
it looks like the position of the colorbar in the figure below,is there any way to solve it?

Accepted Answer

Chunru
Chunru on 21 Jul 2021
You may want to create the colorbar yourself that attached to the data as follows:
n = 50;
x = 100*rand(n,n);
% Add the custom colorbar by extending the data
% gaps of 2 | color bars with width if 2
x = [x nan(n, 2) repmat(linspace(1,100,n)', [1 4])];
contourf(x,'Linestyle','none');
set(gca,'view',([5,30]));
box off
axis off
  2 Comments
Chunru
Chunru on 21 Jul 2021
If you want the perspetive projection:
n = 50;
x = 100*rand(n,n);
% Add the custom colorbar by extending the data
% gaps of 2 | color bars with width if 2
x = [x nan(n, 2) repmat(linspace(1,100,n)', [1 4])];
contourf(x,'Linestyle','none');
%set(gca,'view',([5,30]));
camproj('perspective')
view([0 -10 5]);
%axis([0 100 0 100 0 1]);
box off
axis off

Sign in to comment.

More Answers (1)

Jonas
Jonas on 21 Jul 2021
Edited: Jonas on 21 Jul 2021
try that adding the colors to the image and add artificial labels using text
close all;
x = 100*rand(100,100);
contourf(x,'Linestyle','none');
c = colorbar;
climits=c.Limits; % get limits of original colorbar
x=[x repmat(linspace(climits(1), climits(2),100)',[1 10])]; % add colorbar to image itself
contourf(x,'Linestyle','none'); % plot again
axis([0 110 0 110 0 1]); % extend view to 110 to see the artificial colorbar of width 10
view([5 30]); yticklabels([]); % remove ylabels so we can add our colorbar labels at the position % alternatively you can also edit the yticklabels and inserrt the values from the text() function below
labelPos=0:10:100;
labelVal=linspace(climits(1),climits(2),numel(labelPos));
for labelNr=1:numel(labelPos)
text(115,labelPos(labelNr),0,num2str(round(labelVal(labelNr)))); % add text labels near colorbar
end
xticks(0:10:100); % to prevent the 110 tick value which is not part of the original image

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!