필터 지우기
필터 지우기

how to plot a color-coded matrix

조회 수: 17 (최근 30일)
farfar
farfar 2018년 6월 19일
댓글: Star Strider 2018년 6월 19일
Hello
I have a matrix of data including 3 column,for example:
a=[1 0 3;1 1 4;1 2 4;1 3 5]
x=a(:,1);y=a(:,2);z=a(:,3);
1) in my real data, z is the value associated with (x,y). so I would like to have a 2D chart with x and y value being color-coded based on z value. like all the points in the chart with 1<z<2 are red and 2<z>3 are blue and etc. is there a way to do that?
2) to get what i want, I used the below command for a sample matrix with different z value but the chart is just giving me all points in blue color. what am i missing? what is the best syntax for what I want?
figure
scatter3(x(0<z<=2),y(0<z<=2),z(0<z<=2),'g.')
hold on
scatter3(x(2<z<=4),y(2<z<=4),z(2<z<=4),'k.')
hold on
scatter3(x(4<z<=6),y(4<z<=6),z(4<z<=6),'r.')
Thank you !

채택된 답변

Star Strider
Star Strider 2018년 6월 19일
Try this:
a=[1 0 3;1 1 4;1 2 4;1 3 5]
x=a(:,1);
y=a(:,2);
z=a(:,3);
L1 = (z > 1) & (z <= 3); % Logical Subscripts For Condittion #1
L2 = (z > 3) & (z <= 5); % Logical Subscripts For Condittion #2
figure(1)
scatter3(x(L1), y(L1), z(L1), 'rp')
hold on
scatter3(x(L2), y(L2), z(L2), 'bp')
hold off
grid on
Change the conditions as necessary for your data.
  댓글 수: 3
farfar
farfar 2018년 6월 19일
Thank you very much. one more thing !how can I show the color bar with the plot so the user know I used blue for 0 to 100 and red for 100 to 200?
Star Strider
Star Strider 2018년 6월 19일
As always, my pleasure!
Using legend may be more practical:
legend('0 - 100', '100 - 200')
If you want to use colorbar, try this (after your scatter3 call):
colormap([1 0 0; 0 0 1])
colorbar('Ticks', [0 0.5 1], 'TickLabels',[0 100 200])
This worked when I tested it (in R2018a). You may have to experiment with it.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by