# How can I generate a 2D grid plot my data?

276 views (last 30 days)
MathWorks Support Team on 27 Jun 2009
Edited: Kaid Noureddine on 20 Dec 2017
I have X, Y and Z vectors where X and Y correspond to the x, y co-ordinates and Z is the value at that point. I want to generate a plot in which the elements of a matrix 'Z' are represented by colored squares in a 2-D grid. The color of a square in the grid is determined by the Z value of the square. I want to specify my own colormap for the plot.

### Accepted Answer

MathWorks Support Team on 27 Jun 2009
You can use the PCOLOR function to plot a pseudocolor (checkerboard) plot. The plot is a logically rectangular, two-dimensional grid with vertices at the points [X(i,j), Y(i,j)] where, X and Y are vectors or matrices that specify the spacing of the grid lines. Increase the size of data (Z) and X, Y co-ordinates by one if you want the number of squares to be the size of the data.
%Generate the X and Y grid arrays using the MESHGRID function.
x = [1:6];
y = [1:4];
[X,Y] = meshgrid(x,y)
%Note that size(Z) is the same as size(x) and size(y)
Z = [1 0 -2 1 2 1;2 -2 0 -1 0 1;1 0 0 -2 2 1;1 1 1 1 1 1];
% create a colormap having RGB values of dark green,
%light green, white, dark red and light red.
map2 = [0 1 0; 0 0.8 0;1 1 1;0.6 0 0;1 0 0 ];
%use the user defined colormap for figure.
colormap(map2);
%plot the figure
pcolor(X,Y,Z);
%set the x and y labels
set(gca,'XTick',[1 2 3 4 5 6],'YTick',[1 2 3 4],'XTicklabel',[' ';'a';'b'; 'c'; 'd';'e'],'YTicklabel',[' ';'f';'g';'h']);
%set the color limits
caxis([-2 2])
##### 1 CommentShowHide None
Kaid Noureddine on 20 Dec 2017
Edited: Kaid Noureddine on 20 Dec 2017
Here's an example:
clear; clc
imax = 21;
jmax = 35;
MaxAngle = 270;
Rin = 2.; %radius internal circle
Rex = 5.; %radius external circle
Px=zeros(imax,jmax); Xi=zeros(1,jmax);
Py=zeros(imax,jmax); Yi=zeros(1,jmax);
RI=zeros(imax,jmax); Xe=zeros(1,jmax);
Ye=zeros(1,jmax);
for j= 1:jmax
Theta = MaxAngle*(pi/180.)*(j-1)/double(jmax-1);
Xi(j) = Rin*cos(Theta);
Yi(j) = Rin*sin(Theta);
Xe(j) = Rex*sin(Theta);
Ye(j) = Rex*cos(Theta);
for i = 1: imax
Px(i,j) = Xi(j) + (i-1)/double(imax - 1)* (Rex-Rin)*cos(Theta);
Py(i,j) = Yi(j) + (i-1)/double(imax - 1)* (Rex-Rin)*sin(Theta);
RI(i,j) = sqrt( Px(i,j)^2 +Py(i,j)^2); %Optionnel
end
end
% RI=ones(imax,jmax);
map2 = [1 1 1 ];% map2 = [0 1 0; 0 0.8 0;1 1 1;0.6 0 0;1 0 0 ];
colormap(map2)
pcolor(Px,Py,RI)
grid on

Sign in to comment.

### More Answers (1)

Kaid Noureddine on 20 Dec 2017
Edited: Kaid Noureddine on 20 Dec 2017
clear; clc
imax = 21; % nombre de noeud pour l'angle MaxAngle
jmax = 35; % nombre de noeud le long du segment du rayon Rex-Rin
MaxAngle = 270;
Rin = 2.; %rayon du cercle interne
Rex = 5.; %rayon du cercle externe
Px=zeros(imax,jmax); Xi=zeros(1,jmax); %coordonnées x du cercle interne
Py=zeros(imax,jmax); Yi=zeros(1,jmax); %coordonnées y du cercle interne
RI=zeros(imax,jmax); Xe=zeros(1,jmax); %coordonnées x du cercle externe
Ye=zeros(1,jmax); %coordonnées y du cercle externe
for j= 1:jmax
Theta = MaxAngle*(pi/180.)*(j-1)/double(jmax-1);
Xi(j) = Rin*cos(Theta);
Yi(j) = Rin*sin(Theta);
Xe(j) = Rex*sin(Theta);
Ye(j) = Rex*cos(Theta);
for i = 1: imax
Px(i,j) = Xi(j) + (i-1)/double(imax - 1)* (Rex-Rin)*cos(Theta);
Py(i,j) = Yi(j) + (i-1)/double(imax - 1)* (Rex-Rin)*sin(Theta);
RI(i,j) = sqrt( Px(i,j)^2 +Py(i,j)^2); %Optionnel
end
end
% RI=ones(imax,jmax);
map2 = [1 1 1 ]; % map2 = [0 1 0; 0 0.8 0;1 1 1;0.6 0 0;1 0 0 ];
colormap(hot)% colormap(jet)
pcolor(Px,Py,RI)
grid on
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

R2006b

### Community Treasure Hunt

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

Start Hunting!

Translated by