MATLAB/Octave:调整颜色条轴的刻度position/alignment

MATLAB/Octave: Adjust tick position/alignment for colorbar axis

我的矩阵包含离散值 123,它们(在本例中)是 redgreenblue。颜色条在我最初没有预料到的位置显示这些标签。我想这与分配颜色的方式有关(例如 2.4 不是 green,而是 blue),它不假定离散值。

我希望有一个像“TickLabelAlignment”或类似的设置,但找不到任何东西。所以我不得不"manually"调整位置,这是成功的。然而,是否有更通用的方法来做到这一点?我觉得我正在使用解决方法。

示例:

% 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'});

据我所知,不支持仅支持整数数学的颜色图。你能做的最好的事情就是概括数学:

% 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(三种颜色)计算您在上面使用的位置。