Control colorbar scale in MATLAB

前端 未结 1 1371
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 03:00

Question: How do I specify color transitions in a custom MATLAB colorbar?
Specifically, I\'d like to make the yellow (see below) cover more are

相关标签:
1条回答
  • 2020-12-02 03:35

    I think all that you're missing is a call to caxis to specify the minimum and maximum values to map the color range to:

    caxis([18 23]);
    

    Note that the following line...

    cb.Limits = [18 23];
    

    ... only changes the tick limits displayed on the colorbar, but doesn't change anything about how the data is mapped to the color range. The caxis function is how you control that (in the above case, mapping the value of 18 to one end and the value of 23 to the other). By default, your code was mapping the minimum and maximum values in Z to the color range (20.5 and 23, respectively). When you then set the tick limits on the color bar to a larger range, it just filled it in with the last color in the color map, in this case red. That's why you see so much of it.

    Bonus

    Just because you might be interested, you could also use interpolation via the interp1 function to easily generate your color map like so:

    cmap = interp1([1 0 0; 1 1 0; 0 1 0], linspace(1, 3, 41));
    
    0 讨论(0)
提交回复
热议问题