Search code examples
matlabscattererrorbar

Add errorbar to gscatter Matlab


I want to add a known error bar (vertical) for each x data for the gscatter function. I have plotted the grouped scatter (to specify the colour) with calculated mean. How should I do?

This is my current code

Mydata = readable ('D:\Download\Book1.xlsv);
y = Mydata.Y;
x = Mydata.X;
g = Mydata.Category
size = 10

h = gcatter (x,y,g,'rkgb','X',size);

Solution

  • I don't think that Matlab's scatterplot supports errorbars from within the scatter function itself. I think that some more manual work should be done. Here is a working example with 2 categories, made simple with a loop (you can apply this to much more than just 2 categories)

    Y = [4,3,4,2,10,9,11]; % some invented Y data
    X = [1,2,3,7,6,9,8]; % some invented X data
    groups = [0, 1]; % 2 groups/categories
    G = [0,0,0,1,1,1,1]; % categories of data
    E = [0.1, 0.4, 0.2, 0.5, 0.9, 0.7, 1]; % errors
    colors = {'r', 'k'};
    
    figure, gscatter (X,Y,G,'rk','X',10);
    hold on
    for i = 1:length(groups)
        errorbar(X(G==groups(i)),Y(G==groups(i)),E(G==groups(i)),'LineStyle','None','Color',colors{i})
    end