2016-03-16 143 views
1

我的矩陣包含離散值123,它(在這種情況下)是用於red,​​blue和代碼。彩條顯示這些標籤的位置,我最初不會指望它們是。我想它與方式有關,顏色被分配(例如,2.4不是​​,但是blue),其不假定離散值。MATLAB /倍頻程:調整刻度位置/取向爲彩條軸

我一直希望像「TickLabelAlignment」或類似的設置,但找不到任何東西。所以我不得不「手動」調整位置,這是成功的。然而,是否有更普遍的方式來做到這一點?我覺得我正在使用一種解決方法。

例子:

% set gnuplot as graphics toolkit, set custum colormap and create exemplary matrix 
graphics_toolkit('gnuplot'); 
colormap([1 0 0; 0 1 0; 0 0 1]); 
A = randi([1 3], 5, 5); 

% plot with standard settings 
subplot (2, 1, 1); 
imagesc(A); 
caxis([1 3]); 
mycb = colorbar(); 
set(mycb, 'YTick', [1 2 3], 'YTickLabel', {'red', 'green', 'blue'}); 

% plot with adjusted tick positions (the way I want the colorbar to look like) 
subplot (2, 1, 2); 
imagesc(A); 
caxis([1 3]); 
mycb = colorbar(); 
set(mycb, 'YTick', [4/3 2 8/3], 'YTickLabel', {'red', 'green', 'blue'}); 

enter image description here

回答

1

據我所知,不存在用於僅支持整數運算的colormaps不支持。最好的事情是推廣數學:

% calculate the points where the colour segments start/end 
b = linspace(1,n,n+1); 
% calculate the centers; 
c = mean([b(1:end-1);b(2:end)]); 

其中n = 3(三種顏色)計算您在上面使用的位置。