How to add triangles in colorbar

18 views (last 30 days)
Mike
Mike on 21 Sep 2012
Edited: Paulo Silva on 6 Dec 2018
Hi all,
Is there a way to add triangle at both ends of colorbar. For example, the data range is from -100 to 100, but I only want to show the range from -50 to 50 (say, -100, -50, -40, ..0,...,40, 50, 100). From -100 to -50, and from 50 to 100, I will just use two triangles in both ends of colorbar.
Mike
  3 Comments
Sean de Wolski
Sean de Wolski on 24 Sep 2012
See the additions below.

Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 21 Sep 2012
Edited: Sean de Wolski on 24 Sep 2012
Here is a sample class that does this. Basically it wraps around colorbar giving you a colorbar handle with a few extra options.
classdef pointyColorbar < handle
%Make a colorbar with arrow shaped end
%h = pointyColorbar(minw,maxw,...);
%
%MathWorks - SCd 09/24/2012
%
%Inputs:
% maxw: top base of top arrow
% minw: bottom base of bottom arrow
% Other: input options from colorbar
%
%Outputs:
% h: handle to pointyColorbar object
%
%Methods:
% set: set(obj,'Property','Value') %P/V pairs or other inputs from
% colorbar()
%
%See Also: colorbar
%
%Properties
properties (SetObservable=true)
maxw %top edge of arrow
minw %bottom edge of arrow
end
properties (Access=private)
hCB %Handle to colorbar
end
%Methods
methods
function obj = pointyColorbar(minw,maxw,varargin)
%Very Basic Error Checking
assert(nargin>=2,'Two or more inputs expected')
%Build obj and colorbar
obj.maxw = maxw;
obj.minw = minw;
obj.hCB = colorbar(varargin{:});
%In case the colormap/maxw/minw changes
addlistener(gcf,'Colormap','PostSet',@(~,~)makePointy(obj));
addlistener(obj,'dmaxmin',@(~,~)makePointy(obj));
%Begin!
makePointy(obj);
end
function set(obj,varargin)
%Generic Setter
set(obj.hCB,varargin{:});
makePointy(obj);
end
function set.maxw(obj,val)
obj.maxw = val;
notify(obj,'dmaxmin');
end
function set.minw(obj,val)
obj.minw = val;
notify(obj,'dmaxmin');
end
function delete(obj)
%Delete
delete(obj.hCB);
end
end
methods (Access=protected)
function makePointy(obj)
%Some information
hChild = get(obj.hCB,'Children'); %get image handle
CData = get(hChild,'CData'); %get image data and make it wider so we can pointify the top
%Figure out current settings
if isvector(CData)
%Not pointy
n = numel(CData);
cmap = get(gcf,'Colormap');
if iscolumn(CData);
%Vertical Colorbar
drange = get(hChild,'YData');
lim = 'XLim';
rowflag = false;
elseif isrow(CData)
%Horizontal
drange = get(hChild,'XData');
lim = 'YLim';
rowflag = true;
end
%Build new image matrix
CData = repmat(reshape(cmap(CData,:),[n, 1, 3]),[1 25 1]); %[nx25x3] rgb image
else
%Already pointy!
axpos = get(obj.hCB,'Position');
if axpos(3)<axpos(4)
%Taller than wider -> vertical
drange = get(hChild,'YData');
lim = 'XLim';
CData = CData(:,13,:);
rowflag = false;
else
%Horizontal
drange = get(hChild,'XData');
lim = 'YLim';
CData = CData(13,:,:); %transpose so we can just solve one way
rowflag = true;
end
n = numel(CData)./3; %reset to just size of original CData
%Build new image matrix
CData = repmat(reshape(CData,[n, 1, 3]),[1 25 1]); %[nx25x3] rgb image
end
%Figure out where to blot out
idx = linspace(drange(1),drange(2),n); %This gives where in CData maxw/minw lie
[~,tb] = min(abs(idx-obj.maxw)); %top base
[~,bb] = min(abs(idx-obj.minw)); %bottom base
x = [13 25 25 13 0.5 0.5]; %x coordinates of polygon
y = [0.5 bb tb n tb bb]; %y coordinates of polygon
M = repmat(~poly2mask(x,y,n,25),[1 1 3]); %build mask
CData(M) = 1; %Apply mask
%If we transposed, transpose back
if rowflag
CData = permute(CData,[2 1 3]);
end
%Reset
set(hChild,'CData',CData);
set(hChild,'CDataMapping','Scaled');
set(obj.hCB,lim,[0 1])
end
end
events
dmaxmin; %Changes in max or min
end
end
And here is an example of how to use it:
figure('windowstyle','docked');
imshow('cameraman.tif');
colormap(jet)
h = pointyColorbar(40,210,'location','westoutside');
set(h,'location','northoutside');
h.maxw = 100;
colormap(winter(256));
colormap(copper(256));
h.minw = 25;
h.maxw = 230;
set(h,'location','westoutside');
  1 Comment
Paulo Silva
Paulo Silva on 6 Dec 2018
Edited: Paulo Silva on 6 Dec 2018
sounds interesting, but when running it I got this message:
Index exceeds matrix dimensions.
Error in pointyColorbar/makePointy (line 92)
CData = CData(:,13,:);
Error in pointyColorbar (line 42)
makePointy(obj);
I have never worked with classes before. How could I fix this?
thanks!
\paulo

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!