首页 > 解决方案 > 制作颜色条刻度线标签上方和下方的字符串,删除刻度线 - Matlab

问题描述

我正在尝试为我的图表重现下面的颜色条。具体来说,我正在努力解决的是字符串颜色条轴标题(颜色条上方和下方),其余部分似乎都很好。

所需的彩条 以下是我当前的代码:

    time_begin  = [1981, 1, 1, 0,0,0];
    time_end    = [2010,12,31,23,0,0];
    years       = (time_begin(1):time_end(1))';
    nyears      = length(years);
    TXx1 = randi(100, 288, 192, 30);
    lat = rand(192, 1);
    lon = rand(288, 1);
    time = rand(30,1);
    M = numel(lon);
    N = numel(lat);
    slope1 = zeros(M, N);
    intercept1 = zeros(M, N);
    T = numel(time);
    x = ([ones(T, 1) years]);
    for i = 1 : M
        for j = 1 : N
            y1 = squeeze(TXx1(i, j, :));
            c = regress(y1, x);
            intercept1(i, j) = c(1);
            slope1(i, j) = c(2);      
        end
    end
    TXx2 = randi(100, 288, 192, 30);
    slope2 = zeros(M, N);
    intercept2 = zeros(M, N);
    T = numel(time);
    x = ([ones(T, 1) years]);
    for i = 1 : M
        for j = 1 : N
            y2 = squeeze(TXx2(i, j, :));
            c = regress(y2, x);
            intercept2(i, j) = c(1);
            slope2(i, j) = c(2);      
        end
    end
    figure()
    set(gcf, 'color', 'w'); 
    temp = [slope1(:) slope2(:)];
    temp(temp == 0) = NaN;
    [Q,Qc] = hist3(temp,'Nbins',[100 100],'CDataMode','auto');
    Qx = cell2mat(Qc(1));
    Qy = cell2mat(Qc(2));
    Q = Q';
    Q = Q./trapz(Qy,trapz(Qx,Q,2));
    surf(Qx,Qy,Q,'FaceColor','interp','EdgeColor','none')
    grid off
    set(gca, 'Fontsize', 12, 'Fontweight', 'Bold'); %added
    cmap = jet(500);
    cmap(1,:) = [1,1,1];
    colormap(cmap);
    h=colorbar;
    set(h,'position',[.91 .34 .031 .475]) %[xposition yposition width height].
    set(get(h,'ylabel'),'string','Point density'); 
    set(h,'XTickLabel',{'Low',' ',' ',' ',' ','High',}); 
    view(0,90)

这是我当前的颜色条: 当前颜色条

标签: matlabplotgradientaxiscolorbar

解决方案


替换这一行:

set(h,'XTickLabel',{'Low',' ',' ',' ',' ','High',}); 

和:

h.YTick = h.Limits;
h.YTickLabel = {'Low', 'High'};

这会产生两条刻度线和两个刻度线标签。通过将颜色条的 YTicks 设置为其限制,刻度线与颜色条边界重叠。所以这些被隐藏了,现在不需要删除它们。
但是有这个TickLength属性可以用其他方式使用,即

h.TickLength = 0;

结果:

结果


推荐阅读