function grouping_figure(X, f, fitfunc)
% Visualizes points, their grouping and estimates for each group.

% Copyright 2012 Levente Hunyadi

    validateattributes(X, {'numeric'}, {'nonempty','real','2d'});
    validateattributes(f, {'numeric'}, {'nonempty','integer','vector','>=',1});
    validateattributes(fitfunc, {'function_handle'}, {'scalar'});
    k = max(f);

    colors = {'k','b','r','m','g','c'};
    markers = {'+','o','*','x','^','v','<','>'};
    lineseries = zeros(1,k);
    figure;
    hold('all');
    % plot data first
    for i = 1 : k
        lineseries(i) = dataplot(X(:,f==i), ...
            'Color', colors{mod(i-1,numel(colors))+1}, ...   % cycle through available colors
            'Marker', markers{mod(i-1,numel(markers))+1} ...  % cycle through available markers
        );
    end
    if size(X,1) < 3
        % plot curves second (some curves consist of multiple line series and consume more than one color, style, etc.)
        for i = 1 : k
            theta = fitfunc(X(:,f==i));
            color = get(lineseries(i), 'Color');  % match line color with color of data points
            ax = ancestor(lineseries(i), 'axes');  % use the same figure axes as the data points
            imconic(theta, [], ax, 'Color', color);
        end
    end

    hold('off');
end
