How do I find the handle to the colorbar for a specific axes
42 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
jvc
el 19 de Ag. de 2015
Comentada: Walter Roberson
el 1 de Ag. de 2023
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.
0 comentarios
Respuesta aceptada
Dave White
el 31 de Jul. de 2023
8 years late, but you can also access the hidden Colorbar property 'Axes' which references the peer axis:
cbar=colorbar;
cbar.Axes
1 comentario
Walter Roberson
el 1 de Ag. de 2023
Likewise, if you have an axes handle, then it has a Colorbar property
plot(rand(1,20))
ax = gca
cbar = colorbar;
cbar.Axes == ax
ax.Colorbar == cbar
Más respuestas (2)
Morteza
el 19 de Ag. de 2015
use the specific term for each color bar before making them like:
t = colorbar('peer',gca,'Tag','colorbar1');
get(t)
3 comentarios
Michael G. Baker
el 27 de Nov. de 2018
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
el 1 de Ag. de 2023
If I recall correctly, colorbars used to be children of axes, but are now children of the parent object instead.
Matt J
el 17 de Mzo. de 2022
Editada: Matt J
el 18 de Mzo. de 2022
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
0 comentarios
Ver también
Categorías
Más información sobre Colormaps en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!