JulianAngussmith
JulianAngussmith

Reputation: 75

Matlab Scatter Plot (multiple y values)

I am trying to make a plot with multiple y-values for a single x-value. The code that I have been using plots this in two separate axes.

in = {[26 171 40], [34 32 104 28]}
titles = {'Locker','9u'}
for i  = 1:length(in)
    subplot(1,length(in), i);
    scatter(ones(1,length(in{i})), in{i},'filled')
    set(gca,'XTick',[])
    xlabel(titles{i});
end

How can I plot my data in a single axes? I am not very experienced at using Matlab.

enter image description here

Upvotes: 1

Views: 695

Answers (1)

rinkert
rinkert

Reputation: 6863

You can plot at different x locations, by multiplying the vector with ones by i. And then set the xticks, labels and axis limits appropriately.

As for the colors, you can make a length(in)-by-3 matrix containing the rgb values per category of data. Then provide the appropriate row of this matrix to scatter in the loop.

in = {[26 171 40], [34 32 104 28], randi(200, 5,1), randi(150, 3,1)};
titles = {'Locker','9u', 'A', 'B'};
category_colors = [
    1, 0, 0; % color for 'Locker'
    1, 0, 0; % '9u'
    0, 0, 1; % 'A'
    0, 0, 1; % 'B'
    ];

figure(1); clf;
hold on;
for i  = 1:length(in)
    scatter(ones(1,length(in{i}))*i, in{i},[], category_colors(i,:), 'filled'); % multiply ones with i 
end
set(gca,'XTick',1:length(in));
set(gca, 'XTickLabel', titles);
xlim([0.5 length(in)+0.5])

plot

Upvotes: 3

Related Questions