简体   繁体   中英

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.

在此处输入图像描述

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])

阴谋

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM