how to turn "points" in scatter diagram into "lines"

8 vues (au cours des 30 derniers jours)
Yeping Sun
Yeping Sun le 18 Août 2016
Commenté : Robert le 18 Août 2016

Dear all,

I have two sets of data for two clusters, respectively. These data sets (C_01.txt and C_02.txt) are attached. But define the first columns of these two data set as x1,x2,and the second columns as y1(all elements in y1 are 1),y2(all elements in y2 are 2),respectively, I can draw the attached figure by using

scatter(x1,y1); hold on; scatter(x2,y2);

However, I actually want a image like the following:

That is to say, I need to show each points in the scatter diagram as a vertical line with a certain hight.

Could that be possibly done in matlab?

Réponse acceptée

Star Strider
Star Strider le 18 Août 2016
This will get you started:
fidi1 = fopen('Yeping Sun C_01.txt','rt'); % Begin: Import & Define Data
fidi2 = fopen('Yeping Sun C_02.txt','rt');
x1y1 = textscan(fidi1, '%f%f', 'CollectOutput',1);
x2y2 = textscan(fidi2, '%f%f', 'CollectOutput',1);
fclose(fidi1);
fclose(fidi2);
x1 = x1y1{:}(:,1);
y1 = x1y1{:}(:,2);
x2 = x2y2{:}(:,1);
y2 = x2y2{:}(:,2); % End: Import & Define Data
hgt = 0.2; % Height Of Lines
figure(1)
plot([x1 x1]', [0 hgt]', '-b') % Plot ‘Cluster 1’
hold on
plot([x2 x2]', [hgt 2*hgt]', '-r') % Plot ‘Cluster 2’
plot([0 max(xlim)], hgt*ones(1,2), '-k', 'LineWidth',1.5) % Horizontal Line
plot([0 max(xlim)], 2*hgt*ones(1,2), '-k', 'LineWidth',1.5) % Horizontal Line
plot([1 1]*800, [0 hgt*2], '-k', 'LineWidth',1.5) % Vertical Line
hold off
axis([0 5500 0 2*hgt])
text(50, hgt*0.5, 'Cluster 1', 'Color','b', 'VerticalAlignment','middle', 'FontSize',8)
text(50, hgt*1.5, 'Cluster 2', 'Color','r', 'VerticalAlignment','middle', 'FontSize',8)
set(gca, 'YTick',[]) % Turn Off Y-Ticks
You will have to experiment with the axis limits to get it to look the way you want.

Plus de réponses (1)

Robert
Robert le 18 Août 2016
You could display the data as an image. I see that your x values are all integers in your text files. If this will always be true, you can use them as indices of your array.
img = 0; % initialize with a zero
img(1,x2) = 2; % fill top row (will expand img as needed)
img(2,x1) = 1; % fill bottom row (will expand img as needed)
imagesc(img) % display matrix as image
colormap([1,1,1;0,0,1;1,0,0]) % color zeros white, ones blue, and twos red
  2 commentaires
Yeping Sun
Yeping Sun le 18 Août 2016
sorry, I need to change the x1 and x2 values by multipling them by 0.02, so they will not be integers, and so img(1,x2) = 2 andimg(2,x1) = 1 don't work.
Robert
Robert le 18 Août 2016
You could leave them as integers and adjust the x axis tick labels after plotting.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Scatter Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by