필터 지우기
필터 지우기

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

조회 수: 10 (최근 30일)
Kun
Kun 2014년 5월 8일
댓글: Star Strider 2014년 5월 9일
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?

답변 (3개)

Star Strider
Star Strider 2014년 5월 8일
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
Kun
Kun 2014년 5월 9일
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 2014년 5월 9일
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:

댓글을 달려면 로그인하십시오.


Youssef  Khmou
Youssef Khmou 2014년 5월 8일
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
Youssef  Khmou
Youssef Khmou 2014년 5월 8일
it was only a way to create a matrix similar to that you posted, so surface is not working?
Kun
Kun 2014년 5월 9일
surface gave a similar result as surf did.

댓글을 달려면 로그인하십시오.


Kun
Kun 2014년 5월 8일
편집: Kun 2014년 5월 8일
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
Youssef  Khmou
Youssef Khmou 2014년 5월 8일
try :
M=ones(4,1)*(1:4);
B=kron(M,ones(3));
figure; surface(B);
% shading interp
Kun
Kun 2014년 5월 9일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by