How to generate a non-linear colormap/colorbar?

前端 未结 4 1851
时光说笑
时光说笑 2020-11-30 13:02

I would like to show a non-uniform colorbar as in the first picture. I have tried the code below. \'mycamp4\' is a colormap manually saved. The result is shown as the second

4条回答
  •  天命终不由人
    2020-11-30 13:30

    You can customize the colormap and the ticks in the colorbar, but the colorbar itself is based on linear intervals and you can't change that.

    There's a workaround though.

    lets say you want your colorbar like that in picture 1 with N nonlinear intervals (e.g. color_limits = [1 2 20], if N=2). you have to:

    1) define another variable for your data. e.g.

    data2 = nan(size(data));
    for i=1:N
       b = data>color_limits(i) & data>=color_limits(i+1);
       data2(b) = i-0.5;
    end
    

    Basically you are binning data, using the intervals in color_limits, into a different variable data2 that you will display

    2) make your image of data2

    3) define the colorbar limits: caxis([0 N])

    4) define a colormap with N colors, e.g. colormap(jet(N))

    5) customize the ticks in the colorbar

提交回复
热议问题