% %% Histogram
% [binz, tik] = hist3(y,  {-3:0.1:3, -3:0.1:3}, 'FaceColor', 'interp');
% view(2);
% rotate3d;

% %% Plot 2D
% imagesc(binz/sum(binz(:))); 
% set(gca, 'XTick', 1:5:length(tik{1}), 'XTicklabel', tik{1}(1:5:end)', 'YTick', 1:5:length(tik{1})', 'YTicklabel', tik{2}(1:5:end)'); 
% colorbar;

% %% Foobar
% pkg load statistics
% source('residual_cells.m')

close all
for k=1:size(data,1)
    name=char(data{k, 1})
    residuals=[data{k,2}];
   
    max_e = 5;

    x_to_reject = find(abs(residuals(:,1))<max_e);
    residuals = residuals(x_to_reject,:);

    y_to_reject = find(abs(residuals(:,2))<max_e);
    residuals = residuals(y_to_reject,:);

    % Histogram
    % figure
    [binz, tik] = hist3(residuals,  {-max_e:0.1:max_e, -max_e:0.1:max_e}, 'FaceColor', 'interp');
    % view(2);
    % rotate3d;

    tick_period = round(length(tik{1})/10);

    % Plot 2D
    figure
    subplot(3,2,1)
    % title(name)
    imagesc(binz/sum(binz(:))); 
    set(gca, 
    'XTick', 1:tick_period:length(tik{1}), 
    'XTicklabel', tik{1}(1:tick_period:end)', 
    'YTick', 1:tick_period:length(tik{1})', 
    'YTicklabel', tik{2}(1:tick_period:end)');

    title(name, 'Interpreter', 'none'); 
    colorbar;
    axis equal;
    % saveas(gcf,[name '.png'])

    subplot(3,2,2)
    qqplot(residuals(:,1))
    title([name ' - x'], 'Interpreter', 'none'); 

    subplot(3,2,3)
    qqplot(residuals(:,2))
    title([name ' - y'], 'Interpreter', 'none'); 

    subplot(3,2,5)
    plot(residuals(:,1), tik{1]})
    title([name ' - x'], 'Interpreter', 'none'); 

    subplot(3,2,6)
    plot(residuals(:,2), tik{2]})
    title([name ' - y'], 'Interpreter', 'none'); 

end
