Scatter Plot following a colormap regulation

조회 수: 7 (최근 30일)
Adi Purwandana
Adi Purwandana 2024년 11월 8일
댓글: Star Strider 2024년 11월 8일
Hello there,
I have a datasets of positions (attached). Here are my intention:
  1. Make a scatter plot with different color positions, let's say following "jet" colormap.
  2. Put the station number also along with the scatter plot. Let's say, the value (number of the respected station shown beside the scatter plot of each station.
Here's my code so far which is failed to follow the jet colormap:
clear all;
clc;
A=dir('*.mat');
numprof = length(A);
couleur = jet(numprof); %prefered colormap is jet
for nn=1:length(A)
filename = A(nn).name;
B = load(filename);
xp = B.T.xpos(1);
yp = B.T.ypos(1);
sta = B.T.station(1);
scatter(xp,yp,50,"square",'filled','color',couleur(nn,:), 'DisplayName', string(sta)); % it seems not following the jet colormap setting
hold on;
end
hlgd = legend('Location','best');
title(hlgd, 'Station Number')

채택된 답변

Star Strider
Star Strider 2024년 11월 8일
편집: Star Strider 2024년 11월 8일
The DisplayName name-value pair will use that information for the legend entry for each plotted pont. If you want the station number displayed next to each symbol, usee the text function.
The problem with your original code is that the scatter function syntax is different from other plot-type functions. Give it the command syntax it is expecting and the colors plot correctly.
Try this —
clear all;
clc;
A=dir('*.mat');
numprof = length(A);
couleur = jet(numprof); %prefered colormap is jet
for nn=1:length(A)
filename = A(nn).name;
B = load(filename);
xp = B.T.xpos(1);
yp = B.T.ypos(1);
sta = B.T.station(1);
% scatter(xp,yp,50,"square",'filled','color',couleur(nn,:), 'DisplayName', string(sta)); % it seems not following the jet colormap setting
scatter(xp,yp,50,couleur(nn,:),"square",'filled', 'DisplayName', string(sta)); % it seems not following the jet colormap setting
hold on;
end
hlgd = legend('Location','best');
title(hlgd, 'Station Number')
axis('padded')
% return
clearvars
mats = dir('*.mat');
for k = 1:numel(mats)
filename{k,:} = mats(k).name;;
LD = load(filename{k});
S{k} = LD.T;
end
filename{:}
ans = 'st_01.mat'
ans = 'st_02.mat'
ans = 'st_03.mat'
ans = 'st_04.mat'
% format long
% S{:}
cm = jet(numel(S));
figure
hold on
for k = 1:numel(S)
scatter(S{k}.xpos(1), S{k}.ypos(1), 200, cm(k,:), 'filled', 's')
text(S{k}.xpos(1), S{k}.ypos(1), sprintf(' Station %d',S{k}.station(1)), 'Horiz','left', 'Vert','middle')
end
hold off
grid
axis('padded')
xlabel('xpos')
ylabel('ypos')
As with the previous problem, you can use one loop for this. I used two loops because I wanted to see what the .mat fiiles contained, before I proceeded with the plot.
EDIT — (8 Nov 2024 at 15:53)
Minor tweaks.
.
  댓글 수: 2
Adi Purwandana
Adi Purwandana 2024년 11월 8일
Great! Thanks @Star Strider
Star Strider
Star Strider 2024년 11월 8일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by