Matlab: Using imagesc in grayscale without black - matlab

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.

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

Related

MATLAB pcolor/surf bilinear interpolation (shading interp)

Consider the following MATLAB code:
C = [ 0 0 0 0 0
0 1 2 1 0
0 2 4 2 0
0 1 2 1 0
0 0 0 0 0 ];
pcolor( C );
shading interp;
axis square
Note that C is invariant under 90 degree rotations. Also note this sentence from the help for pcolor:
With shading interp, each cell is colored by bilinear interpolation of the colors at its four vertices, using all elements of C.
However, the plotted image is as follows:
Note that the image is not invariant under 90 degree rotations (consider e.g. the four corners). Now, unless I horribly misunderstand bilinear interpolation, this must be wrong. MATLAB seems to be interpolating on triangles, which is not the same as bilinear interpolation.
Is there any way of working round this MATLAB bug, and getting correct bilinear interpolation? (Other than manually interpolating additional points myself, which still would not cure the issue if one zoomed in far enough.)
I remember having read a few threads concerning this weird behavior in the official Matlab forums, in the past. Unfortunately, I found none right now with a quick search. Anyway... you aren't the first used pointing out that shading interp, used in combination with with pcolor, behaves in a weird manner, creating shapes that don't reflect the underlying data.
The main problem is that shading interp interpolates between data points without caring about how sensible your grid is to smoothing. If you want a result that doesn't look jagged, you have to provide data sampled at a higher resolution:
C = [
0 0 0 0 0
0 1 2 1 0
0 2 4 2 0
0 1 2 1 0
0 0 0 0 0
];
C = interp2(C,5,'cubic');
pcolor(C);
shading interp;
axis square;
This produces an amazing result, and the output doesn't show any artifact or asymmetry:

Scatter plot in Matlab with x-axis ticks not equidistant

Consider the following scatter plot in Matlab
A=[1 0 0 0 2 0 0 0 0 0 0 0 1 2 0 0 0 2 1 200 300]';
xRange = 0: max(A);
prob=zeros(size(xRange,2),1);
for r=1:size(xRange,2)
prob(r) = sum(ismember(A,xRange(r)))/size(A,1);
end
scatter(xRange,prob, 'b');
xlim([-2 max(A)+2])
ylim([-0.5 1.5])
I want to change the way the scatter looks like in order to make it more clear: the idea is to put the following ticks on the x-axis
[-0.5 0 1 2 3 301]
but the tricky part is that they should be equidistant so that I can zoom on the part of the scatter plot with higher values of prob.
Any idea?
One way to achieve this is to transform the data to a new scale using interpolation. Let's say you want the data values
tickVal = [-0.5 0 1 2 3 301];
to appear at the graphical positions
tickPos = 0 : 5;
Determine the graphical positions for all the data by interpolation, and plot the transformed data:
xTransformed = interp1(tickVal, tickPos, xRange);
scatter(xTransformed, prob, 'b');
xlim([min(tickPos), max(tickPos)])
ylim([-0.5 1.5])
Now we have to make sure that the ticks do not reflect the transformed, but the original data values:
set(gca, 'XTick', tickPos)
set(gca, 'XTickLabel', num2cell(tickVal))
The result looks like this:
Is this what you want?

Plotting 2d data in matlab with different colours according to value

I have a 2d array that has values -1 and 1. How do I make the imagesc(lattice) appear in 2 specific colours? Right now it shows red for -1, blue for 1, and green for 0. I want to make the places with 0 appear white.
sounds like you need to set the colormap. If you only have three values, you might set:
cmap = [0 0 1; 1 1 1; 1 0 0]; % sets the colors to blue, white, red\
imagesc(data); colormap(cmap);
You need to set the colormap with those three colors:
cmap = [0 0 1; %// blue
1 1 1; %// white
1 0 0] %// red
colormap(cmap)

LINES(M) unique colors Matlab

I'm using the LINES(M) Matlab function which returns an M-by-3 matrix containing a colormap. The problem is that sometimes it return the same colour twice. As an example I used LINES(8) in the results as shown below the first and last row are the same, so I'm asking how can the returned matrix has unique set of colours, if anyone could advise?
0 0 1
0 0.500000000000000 0
1 0 0
0 0.750000000000000 0.750000000000000
0.750000000000000 0 0.750000000000000
0.750000000000000 0.750000000000000 0
0.250000000000000 0.250000000000000 0.250000000000000
0 0 1
The lines colormap has a maximum of 7 unique colors after which they start to repeat.
>> lines(8)
ans =
0 0 1
0 0.5 0
1 0 0
0 0.75 0.75
0.75 0 0.75
0.75 0.75 0
0.25 0.25 0.25
0 0 1 % <---- starts to repeat
You can always select one of the other colormaps which use interpolation to build as many colors as you want. See doc colormap for a list of supported ones.
Similarly you can build you own colormap using the same linear interpolation technique between a specified number of stop-points.
For example, the jet colormap is constructed by passing through a series of 9 endpoints as shown here, using linear interpolation in between. The hsv colormap is built in a similar manner, only it interpolates across the hue space rather than in RGB. Here is yet another example showing how to build a custom divergent colormap with red-white-blue endpoints.
The lines color map has only 7 unique colors (as you have already spotted).
If you need more than 7 unique colors, you'll have to create a map by yourself.
One option is using rand:
>> rCmap = rand( n, 3 ); % create a random map with n colors - usually unique.

How can I display a 2D binary matrix as a black & white plot?

I have a 2D binary matrix that I want to display as a black and white plot. For example, let's say I have a 4-by-4 matrix as follows:
1 1 0 1
0 0 1 0
1 1 0 1
1 0 0 0
How can this be plotted as a black and white matrix? Some of my input binary matrices are of size 100-by-9, so I would ideally need a solution that generalizes to different sized matrices.
If you want to make a crossword-type plot as shown here (with grid lines and black and white squares) you can use the imagesc function, a gray colormap, and modify the axes properties like so:
mat = [1 1 0 1; 0 0 1 0; 1 1 0 1; 1 0 0 0]; % Your sample matrix
[r, c] = size(mat); % Get the matrix size
imagesc((1:c)+0.5, (1:r)+0.5, mat); % Plot the image
colormap(gray); % Use a gray colormap
axis equal % Make axes grid sizes equal
set(gca, 'XTick', 1:(c+1), 'YTick', 1:(r+1), ... % Change some axes properties
'XLim', [1 c+1], 'YLim', [1 r+1], ...
'GridLineStyle', '-', 'XGrid', 'on', 'YGrid', 'on');
And here's the image you should get:
I'm not sure if I got your question right, but you may try the image function, like this:
A = [ 1 1 0; 1 0 1; 1 1 1 ];
colormap([0 0 0; 1 1 1 ]);
image(A .* 255);
Try the spy function to start with perhaps.