How to add triangles in colorbar
20 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
something like the colorbar on the right ( http://www.google.com/imgres?num=10&hl=en&biw=1280&bih=675&tbm=isch&tbnid=EDm8DA5OQpzh5M:&imgrefurl=http://idldatapoint.com/2012/05/24/idl-8-2-released/&docid=pVBmd4B4TWQPOM&imgurl=http://idldatapoint.files.wordpress.com/2012/05/ng_quick_colorbars.png&w=366&h=683&ei=H85cUJLlJYeq8ASexIGAAw&zoom=1&iact=rc&dur=491&sig=101586727009009858731&sqi=2&page=1&tbnh=160&tbnw=86&start=0&ndsp=17&ved=1t:429,r:2,s:0,i:77&tx=44&ty=87) Thanks.
Mike
3 Commenti
Risposte (1)
Sean de Wolski
il 21 Set 2012
Modificato: Sean de Wolski
il 24 Set 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 Commento
Paulo Silva
il 6 Dic 2018
Modificato: Paulo Silva
il 6 Dic 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
Vedere anche
Categorie
Scopri di più su Colorbar in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!