MATLAB/倍频程:调整色条轴的刻度位置/对齐

MATLAB/倍频程:调整色条轴的刻度位置/对齐,matlab,alignment,octave,colorbar,Matlab,Alignment,Octave,Colorbar,我的矩阵包含离散值1、2和3,它们(在本例中)分别是红色、绿色和蓝色的代码。颜色栏显示了这些标签的位置,而我最初并没有想到它们会出现在这些位置。我想这与分配颜色的方式有关(例如,2.4不是绿色,而是蓝色),它不假设离散值 我希望设置为“TickLabelAlignment”或类似的设置,但找不到任何内容。所以我不得不“手动”调整位置,这是成功的。然而,有没有更普遍的方法来做到这一点?我觉得我在使用变通方法 例如: % set gnuplot as graphics toolkit, set cu

我的矩阵包含离散值
1
2
3
,它们(在本例中)分别是
红色
绿色
蓝色
的代码。颜色栏显示了这些标签的位置,而我最初并没有想到它们会出现在这些位置。我想这与分配颜色的方式有关(例如,
2.4
不是
绿色,而是
蓝色),它不假设离散值

我希望设置为“
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'});

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

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