Adamosphere
Adamosphere

Reputation: 77

Matlab: Using imagesc in grayscale without black

Beginning with a simple 2D matrix m of either 0 or 1 as an example:

m = [ 0 0 0 1 1
      0 1 1 1 0
      1 1 0 0 1
      0 0 0 1 0 ] 

How would I get this image to be displayed in a figure as only white and one gray using imagesc()? Currently, my code would is something like this:

imagesc(m)
colormap(gray)
colorbar

I've experimented with various ways to adjust the colormap and set limits on which values are used with CLim, but I have not found a way to limit the actual colors themselves so that instead of having white and black for values of 0 and 1, we have a white and (light) gray value for 1 and 0, respectively. Any quick and easy ways to do this?

Note: I should also mention that I want these so I can overlay a a contour plot of the same dimensions (but different values) and by using black, so I'm not sure if that would factor into any answers, but I am also open to suggestions with that in mind.

Upvotes: 0

Views: 17748

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112689

imagesc(m)
cmap = [.7 .7 .7 %// light gray
        1  1  1] %// white
colormap(cmap)
colorbar('Ytick',[.25 .75],'Yticklabel',[0 1]) %// only two values in colorbar

enter image description here

Upvotes: 3

Related Questions