Search code examples
matlabplotmatlab-figurematlab-guidecolorbar

Visualize a matrix with small numbers


I have generated a random matrix. I want to show it on a plot and see different values with different colors.

phi = zeros(10,10);
for i= 1:length(phi)
for j=1:length(phi)
    phi(i,j) = .5 + .2*(.5-rand);
end
end
Image(phi)

I have been using colorbar and caxis to set the range of colorbar, I already have read some other posts, but it does not work in my case and I don't know why. I want to set the colorbar from zero 0 to 1 with 100 units. Although, I have tried

Image(phi*10)

which give me something similar to what I want but it does not show difference between close numbers like 0.42 and 0.45. Thanks in advance for your help.


Solution

  • First, no need for the for loop, this will do the same:

    phi=.5+.2.*(.5-rand(10));
    imagesc(phi);
    

    second, use:

    colormap(jet(16)); 
    colorbar;
    

    this will discretize your colormap to how many levels (or bin intensities) you want, I chose 16, but you can play with it and see.It also uses the maximum and minimum values as the extreme points of the colormap.

    Last, if this is still not enough and you need a higher dynamic range, you will need to edit the colormap or combine several colormaps. One way to do this for example is by combining b\w with color, for example

    colormap([jet(8); bone(8)] );
    

    enter image description here etc...