Scatter Plot following a colormap regulation

3 Ansichten (letzte 30 Tage)
Adi Purwandana
Adi Purwandana am 8 Nov. 2024
Kommentiert: Star Strider am 8 Nov. 2024
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')

Akzeptierte Antwort

Star Strider
Star Strider am 8 Nov. 2024
Bearbeitet: Star Strider am 8 Nov. 2024
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.
.

Weitere Antworten (0)

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by