在中心放置刻度标签

Placing tick labels in the centre

我已经为特定的颜色映射编写了一个脚本,如下所示:

white=[1 1 1];
yellow=[1 1 0];
orange=[1 0.5 0];
red=[1 0 0];
black=[0 0 0];
custom_map = [white; yellow; orange; red; black]; 
colorbar('YTickLabel',{'None','Moderate','Strong','Severe','Extreme'})
caxis([1,5]);

我希望将刻度标签放置在颜色条的中央,即标签 'white' 将放置在颜色条白色部分的中间。到目前为止,我很难做到这一点。

有没有简单的方法来做到这一点?

Luis 在 comment below the answer to your earlier question 中已经给出了提示。由于正确设置颜色条的样式有点棘手,这里有一个完整的示例:

% Custom colormap
white = [1 1 1];
yellow = [1 1 0];
orange = [1 0.5 0];
red = [1 0 0];
black = [0 0 0];
custom_map = [white; yellow; orange; red; black]; 
n_colors = size(custom_map, 1);

% Generate and show sample data; apply custom colormap
a = rand(20, 20);
imagesc(a);
colormap(custom_map);

% Add styled colorbar
vmin = 0;
vmax = 1;
v_range = vmax - vmin;
ticks = v_range*(vmin+1/(2*n_colors)):v_range*(1/n_colors):vmax;
ticklabels = {'white', 'yellow', 'orange', 'red', 'black'};
colorbar('Ticks', ticks, 'TickLabels', ticklabels);
% Also works, and Octave compatible:
% colorbar('ytick', ticks, 'yticklabel', ticklabels);
caxis([vmin, vmax]);

为了正确对齐刻度,您必须包含所需值范围的最小值和最大值(示例中的 vminvmax),以及范围本身(v_range), 和颜色数 (n_colors).

这是一些输出(Octave 5.1.0,但也在 MATLAB Online 中测试):

实施应该非常灵活。这是相同的数据,但不同 vmax = 3:

或者,这是具有两种额外颜色的相同数据:

希望对您有所帮助!