How to plot a graphic with different markers in a scatter plot?

178 visualizaciones (últimos 30 días)
Hi;
I want to plot a graphic with different shaped markers?
I send a graphic in the attached file. As you see there are 9 dots with grouped as A, B and C. I want to plot each of them with a different marker.
For example;
Dot 1: circle
Dot 2: square
Dot 3: diamond
Dot 4: cross
etc...
This is my Matlab code:
clear;
load -ascii test1.txt;
a=test1(:,1);
b=test1(:,2);
c=test1(:,3);
d=test1(:,4);
e=test1(:,5);
f=test1(:,6);
scatter(a,b, 'r', 'Linewidth',15);
hold on;
scatter(c,d, 'b', 'Linewidth',15);
hold on;
scatter(e,f, 'g', 'Linewidth',15);
xlim ([0.8 1.3]);
ylim([100 180]);
set(gca,'Fontsize',24,'Linewidth',2);
grid;
How can I plot each dot with a different marker shape? An also how can I show these 9 dots in legend?
Thanks a lot..
  4 comentarios
Adam
Adam el 17 de Oct. de 2019
You would need to create 9 different scatter plots and plot them all together on the same axes.
SEVAL DONERTAS
SEVAL DONERTAS el 17 de Oct. de 2019
I attch my ."txt file" and my matlab code in the attached file.
How can I create 9 different scatter plots together in same axes?
Regards,

Iniciar sesión para comentar.

Respuesta aceptada

meghannmarie
meghannmarie el 17 de Oct. de 2019
Editada: meghannmarie el 17 de Oct. de 2019
Is this what you mean? This is assuming that a,b,c,d,e,f are 3 element vectors.
clear;
load -ascii test1.txt;
a=test1(:,1);
b=test1(:,2);
c=test1(:,3);
d=test1(:,4);
e=test1(:,5);
f=test1(:,6);
fmt = {'*','o','^','.','+','x','s','p','h'};
f = 0;
for n = 1:3
scatter(a(n),b(n),['r' fmt{f+1}], 'Linewidth',15);
hold on;
scatter(c(n),d(n), ['b' fmt{f+2}], 'Linewidth',15);
hold on;
scatter(e(n),f(n), ['g' fmt{f+3}], 'Linewidth',15);
hold on;
f = f + 3;
end
xlim ([0.8 1.3]);
ylim([100 180]);
set(gca,'Fontsize',24,'Linewidth',2);
grid;

Más respuestas (2)

Adam Danz
Adam Danz el 17 de Oct. de 2019
Editada: Adam Danz el 17 de Oct. de 2019
"I want to plot each "9 dots" in the graphic with different markers."
Use group-scatter gscatter(x,y,g,clr,sym,siz).
Each coordinate will be its own group but it's color will be defined by an additional grouping variable. Since gscatter() returns a handle for each group, you can change the marker for each coordinate. See inline comments for more detail.
% Your raw data; an nxm matrix where every 2 columns define (x,y) coordinates
% for a particular group (even number of columns)
%test1 = randi(10,3,6); %fake data
load -ascii test1.txt; % Your actual data
% Reshape the data into (x,y) pairs (2 columns) and identify the groups for
% each coordinate
test1xy = reshape(test1',2,numel(test1)/2)'; %[x,y] columns
test1Grp = repmat((1:(size(test1,2)/2))',size(test1,1),1); %groups
% set group colors
color = hsv(size(test1,1));
color = color(test1Grp,:);
% Plot the data so that each coordinate gets its own handle and each group
% gets it's own color
gsh = gscatter(test1xy(:,1), test1xy(:,2), 1:size(test1xy,1), color, '.', 15, false);
% Set markers
markers = {'s' '^' 'p' 'h' 'v' 'd' 'x' 'o' '+'}; %must match number of groups!
set(gsh,{'Marker'}, markers(:))
% fill markers
set(gsh,{'MarkerFaceColor'}, mat2cell(color,ones(size(color,1),1),3))
Results (from your data)
191017 104450-Figure 1.png

dpb
dpb el 17 de Oct. de 2019
"plot each "9 dots" in the graphic with "different marker.". Not each group."
Wow. But, ok...that's simpler with plot than it is with scatter owing to weakness in the implementation of the scatter plot...it's designed for groups and can only accept vector, not array inputs, sadly.
mkrs = {'*','o','^','.','+','x','s','p','h'}.'; % NB: column, NOT row
x=test1(:,1:2:end); % convenient shorthand
y=test1(:,2:2:end); % variable names
x=x(:).'; y=y(:).'; % turn into row vectors
dmy=nan(size(x)); % a placeholder to fool plot()
hL=plot([x;dmy],[y;dmy],'.','markersize',15); % create the line, default marker
set(hL,{'Marker'},mkrs) % and set the markers to the list
The above also has to have a workaround to avoid the use of looping or arrayfun() to add multiple individual points to the plot. For arrays, plot creates a line for each column; but it ignores orientation if the input is a vector and only creates a single line handle. We need one for each point here to set the marker individually so the NaN dummy vector creates the non-showing data to pass an array so hL will be an array of line handles.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by