How do I plot the data as patches rather than vertices in a grid or a mesh?

9 views (last 30 days)
I have been trying to visualize a set of stochastic data like the matrix below:
1 1 1 2 2 3 3;
1 1 1 2 2 3 3;
1 1 1 1 2 2 3;
1 1 1 1 2 2 3;
1 1 1 1 2 2 3;
I want to create something similar to a chess board, with 1 being blue, 2 being yellow and 3 being red (see the attached figure).
I have tried contourf and surf, but they all put data on the vertices of the lattices, so if the shading function is faceted, it will give
And if the shading is interp, it will be
The boundaries between ones, twos and threes is an artificial transition at the blank area where no data exist.
I then tried adding an additional row and column, or, as Youssef KHMOU suggested, using the kron function. Both ways gave the right lattice pattern, but then I had to manually adjust the position of the ticks to produce the first graph, which is what I need.
Is there a better way to do this?

Answers (3)

Star Strider
Star Strider on 8 May 2014
Use the pcolor function.
A = 1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 2 2 1 1 1 2;
2 2 2 2 3 3 2;
2 2 3 3 3 3 3];
figure(1)
pcolor(A)
axis equal
colormap(gray)
  2 Comments
Kun
Kun on 9 May 2014
I tried you way, but the result were the same. Guess I did not make my question clear.I will rephrase it.
Star Strider
Star Strider on 9 May 2014
This may be what you want:
A = [1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 2 2 1 1 1 2;
2 2 2 2 3 3 2;
2 2 3 3 3 3 3];
x = [0:6]+0.5;
y = [0:4]+0.5;
figure(1)
image(x, y, A)
axis equal tight
colormap(gray(3))
grid on
h = get(gca)
set(gca, 'YTick', 0:5, 'GridLineStyle','-')
produces:

Sign in to comment.


Youssef  Khmou
Youssef Khmou on 8 May 2014
Tensor product can be efficient in representing each number with different color, but try this way i think it is convenient to your description :
M=ones(4,1)*(1:4)
surface(M)
  3 Comments
Youssef  Khmou
Youssef Khmou on 8 May 2014
it was only a way to create a matrix similar to that you posted, so surface is not working?

Sign in to comment.


Kun
Kun on 8 May 2014
Edited: Kun on 8 May 2014
I just tried adding an additional row and column to the original matrix and then using the surface function. It gave the correct chess board pattern I need, but the ticks on x,y-axis were still assigned to the vertices, so I had to shift them to match the location of the patches. But this is tedious if the size of the data matrix is huge. Does anyone have better solutions?
  2 Comments
Kun
Kun on 9 May 2014
Yeah, it worked. But I still need to adjust the position of the ticks on both axes. Perhaps the only way to get the graph I want is to do it in two steps.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!