PLot two cluster plot

1 vue (au cours des 30 derniers jours)
sm m
sm m le 26 Juil 2015
I want to get a cluster plot as I would have 2 set of values One set which falls as positive set i.e y1 and the other set of values which fall in negative set i.e y2
My code:
> y1 = [11.0 10.8 11.0 11.10 11.9 9.9] ;
>> y2 = [14.0; 16.8; 18.0; 19.10; 21.9; 29.9] ;
>> figure
>> plot(y1,'ok','MarkerFaceColor','k')
>> hold on
>> plot(y2,'ok','MarkerFaceColor','r')
This works and i get a plot like this fig attached
However i am looking for something like getting two clusters against two values on x axis ie y1 and y2
and so i define the names for y1 and y2 and I try to plot them
names = {'Postive =y1' 'Negative =y2'};
plot(names,y1,'ok','MarkerFaceColor','k')
hold on
set(gca,'xtick',[1:2],'xticklabel',names)
plot(names,y1,'ok','MarkerFaceColor','k')
I still get numbers on my x axis whereas I want names on x axis

Réponses (1)

Abhipsa
Abhipsa le 29 Avr 2025
Hi @sm m,
I understand that you would like to plot two sets of data (“y1” and “y2”) as clusters on the x-axis, and instead of displaying numbers, you would like to show the labels "Positive = y1" and "Negative = y2".
In the approach you shared, the variable “names” is beging used directly as x-values in the “plot” function, which caused MATLAB to interpret them as categorical data or indices. As a result, numbers appeared on the x-axis instead of the intended labels.
Here’s a corrected version of the code to achieve the desired behaviour:
y1 = [11.0 10.8 11.0 11.10 11.9 9.9];
y2 = [14.0 16.8 18.0 19.10 21.9 29.9];
names = {'Positive = y1', 'Negative = y2'};
figure
hold on
% Plot y1 points at x = 1
plot(ones(size(y1)), y1, 'ok', 'MarkerFaceColor', 'k')
% Plot y2 points at x = 2
plot(2 * ones(size(y2)), y2, 'ok', 'MarkerFaceColor', 'r')
% Set x-axis labels and ticks
set(gca, 'xtick', [1 2], 'xticklabel', names)
% Set limits for x-axis for better spacing
xlim([0.5 2.5])
ylabel('Values')
title('Clustered plot of Positive and Negative sets')
hold off
Using this approach, you will be able to see the labels "Positive = y1" and "Negative = y2" on the x-axis, along with the clustered data points as intended.
output
You can use the below command to learn more about “plot” function:
>>doc plot
I hope this resolves your query.

Community Treasure Hunt

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

Start Hunting!

Translated by