Search code examples
matlabplotmatlab-figurecategorical-datascatter

Plot categorical x axis


I have a data set (shown in picture from excel):

enter image description here

and I want to make a scatter plot with the categories along the x-axis:

enter image description here

My current code is:

Mydata= readtable('D:\Download\Book1.xlsv');
y= Mydata.Group1;
x=Mydata.Y;
size= 50;
scatter (x,y,size,'magenta','filled','square');

hold on

y= Mydata.Group2;
scatter(x,y,size,'red','filled','d');

y= Mydata.Group3;
scatter(x,y,size,'b','filled','p');

y= Mydata.Group4;
scatter(x,y,size,'yellow','filled','h');

y=Mydata.Group5;
scatter(x,y,size,'k','filled','o');

hold off

With this current code, all data were plotted in one line not like in the picture. I also want to add an error bar for each data later. How can I achieve this?


Solution

  • Here is one way to plot data points for a category and color them by series.

    % generate numbers
    X = rand(3,4);
    
    cols = {'r','b','g'}
    num_categories = size(X)(2);
    num_series = size(X)(1);
    
    labels = cell(num_categories,1)
    legends = cell(num_series,1)
    
    figure;
    hold on;
    
    % plot series
    for i=1:num_series
      scatter(1:num_categories,X(i,:),cols{i})
      legends{i,1} = ['series ', num2str(i)];
    end
    
    % generate labels for categories
    for i=1:num_categories
      labels{i,1} = ['category ', num2str(i)];
    end
    
    set(gca, 'xtick',1:num_categories)
    set(gca, 'xticklabel', labels)
    axis([0, num_categories+1, 0, 1]);
    legend(legends)
    

    Can you elaborate what you mean with error bars and where you would want them to be? Do you want errors per category and series?