How to plot number of a matrix with colorcode?
조회 수: 1 (최근 30일)
이전 댓글 표시
A matrix with dimension 2193 * 2, where 1st column is depth and 2nd column is number ( 1 to 6). I want to plot these number with specific color like 1 is green, 2 is black, 3 is cyan, 4 is red, 5 is blue, and 6 is yellow. How to plot it with specific color against depth. Please, help me with useful information. thanks
댓글 수: 2
DGM
2021년 12월 16일
편집: DGM
2021년 12월 16일
What exactly do you want? When you say "plot it with specific color against depth", you realize that the result for each category will be constant-valued, right?
A = xlsread('Book1.xlsx');
scatter(A(:,1),A(:,2),10,A(:,2))
cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];
colormap(cmap)
I'm assuming you want something other than this. Maybe something like this?
figure;
A = xlsread('Book1.xlsx');
idx = 1:size(A,1);
uv = unique(A(:,2));
hold on
for k = 1:numel(uv)
mask = A(:,2) == uv(k);
plot(idx(mask),A(mask,1),'o');
end
cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];
set(gca,'colororder',cmap)
Not that I think that's particularly readable either.
채택된 답변
DGM
2021년 12월 16일
편집: DGM
2021년 12월 16일
You can use pcolor()
A = xlsread('Book1.xlsx');
w = 100;
h = pcolor(1:w,A(:,1),repmat(A(:,2),[1 w]));
set(h,'edgecolor','none')
set(gca,'ydir','reverse','xticklabel',{})
axis equal
xlim([1 w])
cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];
colormap(cmap)
Something like that?
댓글 수: 3
DGM
2021년 12월 16일
w sets the width of the plot box in data units. You can adjust it to make the plot wider or narrower.
추가 답변 (3개)
Walter Roberson
2021년 12월 16일
편집: Walter Roberson
2021년 12월 16일
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/835655/Book1.xlsx';
A = readmatrix(filename);
N = size(A,1);
pointsize = 25;
cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];
scatter(1:N, A(:,1), pointsize, A(:,2));
colormap(cmap);
colorbar()
ylabel('Depth')
xlabel('row number')
scatter(A(:,2), A(:,1), pointsize, A(:,2))
colormap(cmap)
xlabel('category')
ylabel('Depth')
boxplot(A(:,1), A(:,2))
xlabel('category')
ylabel('depth')
댓글 수: 0
DGM
2021년 12월 16일
편집: DGM
2021년 12월 16일
Here's my latest guess:
A = xlsread('Book1.xlsx');
uv = unique(A(:,2));
hold on
for k = 1:numel(uv)
mask = A(:,2) == uv(k);
plot(A(mask,1));
end
cmap = [0 1 0; 0 0 0; 0 1 1; 1 0 0; 0 0 1; 1 1 0];
set(gca,'colororder',cmap)
ylabel('depth')
xlabel('sample number')
Here, depth is plotted against the number of each sample in the respective categories. If depth is plotted against the sample number across all categories (i.e. the row subscript), then the result is the same as Walter's first example and the second example I gave.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!