How do I find the handle to the colorbar for a specific axes

조회 수: 50 (최근 30일)
jvc
jvc 2015년 8월 19일
댓글: Walter Roberson 2023년 8월 1일
I would like to know how to programatically find the handle to the colorbar for a specific axis in a figure which contains several subplots which each of which might or might not have an associated colorbar. Since colorbars are children of the figure it is not obvious to me how I get the handle of the correct colorbar. I can easily get a list of all of the colorbar handles, but I do not see any property value which indicates which bar goes with which axis.

채택된 답변

Dave White
Dave White 2023년 7월 31일
8 years late, but you can also access the hidden Colorbar property 'Axes' which references the peer axis:
cbar=colorbar;
cbar.Axes
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 8월 1일
Likewise, if you have an axes handle, then it has a Colorbar property
plot(rand(1,20))
ax = gca
ax =
Axes with properties: XLim: [0 20] YLim: [0 1] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
cbar = colorbar;
cbar.Axes == ax
ans = logical
1
ax.Colorbar == cbar
ans = logical
1

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Morteza
Morteza 2015년 8월 19일
use the specific term for each color bar before making them like:
t = colorbar('peer',gca,'Tag','colorbar1');
get(t)
  댓글 수: 3
Michael G. Baker
Michael G. Baker 2018년 11월 27일
C = findall(gcf,'type','ColorBar');
if isempty(C)
display('No ColorBar.');
end
ColorBars are apparently children of the Figure object, rather than the Axes. I'm not sure how Matlab keeps the colorbars associated with the relevant axes, though. If I need to modify positions and settings later on, I set the object Tags to the same value and use the findall command.
Walter Roberson
Walter Roberson 2023년 8월 1일
If I recall correctly, colorbars used to be children of axes, but are now children of the parent object instead.

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2022년 3월 17일
편집: Matt J 2022년 3월 18일
Here is an excruciatingly roundabout method.
function [Hcb,tf]=getColorbars(hAxes)
%Return colorbar handles for axes with colorbars, and optionally a logical
%indicator array telling which axes have colorbars.
%
%IN:
%
% hAxes: an array of axes handles
%
%OUT:
%
% Hcb: an array of colorbar handles corresponding to the elements of
% hAxes. If hAxes(i) has no colorbar, then Hcb(i) will be
% an empty GraphicsPlaceHolder.
%
% tf: a logical array indicating which elements of hAxes have
% colorbars.
Hcb=gobjects(size(hAxes));
tf=false(size(hAxes));
N=numel(Hcb);
for i=1:N
Hfig=ancestor(hAxes(i),'figure');
before=numel(findobj(Hfig,'Type','colorbar'));
colorbar(hAxes(i),'off');
after= numel(findobj(Hfig,'Type','colorbar'));
if after<before %colorbar was present
Hcb(i)=colorbar(hAxes(i));
tf(i)=1;
end
end
end

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by