Create checkerboard plot from a matrix where *each point* is represented by a colored square

79 visualizzazioni (ultimi 30 giorni)
I would like to represent a set of 2-d data (3 x 4 matrix) visually using a "checkerboard"-type plot. However the options for doing this reduce each dimension by 1, so the checkerboard is 2 x 3.
Instead I would like the relative magnitude of each data point to be represented by the intensity of color on a color scale.
I have tried pcolor and surf, but these do not seem to achieve the desired result.
My guess is there should be a simple way to do this, however I have not been able to figure it out.

Risposta accettata

Image Analyst
Image Analyst il 3 Set 2015
Perhaps using colormap() and image() or imshow():
% Create a 3x4 array of sample data in the range of 0-255.
data = randi(255, 3, 4)
% Display it.
image(data);
% Initialize a color map array of 256 colors.
colorMap = jet(256);
% Apply the colormap and show the colorbar
colormap(colorMap);
colorbar;
  4 Commenti
DGM
DGM il 26 Dic 2022
Modificato: DGM il 26 Dic 2022
This line
data = randi(255, 3, 4)
draws from 1:255, not 0:255.
When using image()/imshow(), floating point inputs are mapped from 1:N, whereas integer-class inputs are mapped from 0:N-1. So if you want a direct mapping to a 256-row color table, your image needs to span 1:256, not 1:255 or 0:255.
Consider the examples:
% Create a 3x4 image containing integers from 1:12 (floating point)
data = reshape(1:12,3,4);
% Display it.
image(data);
% Initialize a color map array of 12 colors.
colorMap = jet(12);
% Apply the colormap and show the colorbar
colormap(colorMap);
colorbar;
% Create a 3x4 image containing integers from 0:11 (integer-class)
data = uint8(reshape(0:11,3,4));
% Display it.
image(data);
% Initialize a color map array of 12 colors.
colorMap = jet(12);
% Apply the colormap and show the colorbar
colormap(colorMap);
colorbar;
Those both map correctly, but these don't.
% Create a 3x4 image containing integers from 0:11 (floating point)
data = reshape(0:11,3,4);
% Display it.
image(data);
% Initialize a color map array of 12 colors.
colorMap = jet(12);
% Apply the colormap and show the colorbar
colormap(colorMap);
colorbar;
% Create a 3x4 image containing integers from 1:12 (integer-class)
data = uint8(reshape(1:12,3,4));
% Display it.
image(data);
% Initialize a color map array of 12 colors.
colorMap = jet(12);
% Apply the colormap and show the colorbar
colormap(colorMap);
colorbar;
There are other ways around this, but class is something to keep in mind when dealing with pseudocolor images.

Accedi per commentare.

Più risposte (1)

Cam Salzberger
Cam Salzberger il 3 Set 2015
Modificato: Cam Salzberger il 4 Set 2015
Hello,
I understand that you would like to display a colored grid, where each grid square's color is determined by a data matrix. The pcolor function with 'faceted' shading can actually do this, though you will have to add a throw-away row and column to the data matrix so that they can be dropped without removing data.
C = [0 1 2 3 ; 1 2 3 4 ; 2 3 4 5]; % Or whatever data
C = [[C zeros(size(C,1),1)] ; zeros(1,size(C,2)+1)];
pcolor(C)
If the data is corresponding to particular X-Y coordinates, you can take this a step further and plot the grid so that each grid square covers the corresponding coordinates. This even works if the data is not evenly spaced.
x = [20 22 26 34]; % Random data
y = [0 4 6];
C = [0 1 2 3 ; 1 2 3 4 ; 2 3 4 5];
xSplit = diff(x)/2; % Find edge points
ySplit = diff(y)/2;
xEdges = [x(1)-xSplit(1) x(2:end)-xSplit x(end)+xSplit(end)];
yEdges = [y(1)-ySplit(1) y(2:end)-ySplit y(end)+ySplit(end)];
[XGrid, YGrid] = meshgrid(xEdges,yEdges);
YGrid = flipud(YGrid); % To match expected behavior
C = [[C zeros(size(C,1),1)] ; zeros(1,size(C,2)+1)];% Last row/col ignored
pcolor(XGrid,YGrid,C)
hold on % Plot original data points
[X,Y] = meshgrid(x,y);
Y = flipud(Y);
plot(X,Y,'or')
You can always change the colormap for that particular figure as well, to change how the data values are mapped to color.
I hope this helps with your data visualization.
-Cam
  1 Commento
Taylor Juran
Taylor Juran il 3 Dic 2019
Hi Cam,
Thanks, that does exactly what I want!
Does anyone know if it is also possible to set a specific value to a specific color that isnt on the colorbar? For example, I want to use colormap jet, but i want to set all C values = 0 to be white.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by