function errIntegrityPlot(err, errEst, filePrefix, titleStr, MaxErr, Step)

%Step   = 0.25;
%MaxErr = 20;

%err = sqrt(ENU(:,1).^2 + ENU(:,2).^2);


isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
if(isOctave)
  figure("visible","off");
else
  figure;
end
plot(err,errEst,'.');

if(isOctave)
  figure("visible","off");
else
  figure;
end
plot(err);
hold;
plot(errEst,'r');
legend('Error','Error Estimate','location','best');
ylabel('Error & Estimated Error [m]');
grid;

err = err(:);
errEst = errEst(:);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hard limit the outliers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
i = find(err > MaxErr);
if(length(i) > 0)
  err(i) = MaxErr-Step;
end

i = find(errEst > MaxErr);
if(length(i) > 0)
  errEst(i) = MaxErr-Step;
end


if(isOctave)
  figure("visible","off");
else
  figure;
end

dat = [err,errEst];
ctrs{1} = 0:Step:MaxErr;
ctrs{2} = 0:Step:MaxErr;
if(exist('OCTAVE_VERSION', 'builtin') ~= 0)
  n1 = hist3(dat,'Ctrs',ctrs);
else
  % Unless you have the Stats package installed you won't have hist3,
  % a renamed version from Octave is available
  n1 = hist3(dat,'ctrs',ctrs);
end
n1 = n1';
n1 = log10(n1);

xb = ctrs{1};
yb = ctrs{2};

% For octave NaN does not work in pcolor to set
% a transparent value. Therefore we'll find and remove
% all zeros. However, log10(1) = 0 so adjust slightly so
% this data does not get lost.
if(exist('OCTAVE_VERSION', 'builtin') ~= 0)
  i = find(n1 == 0);
  if(length(i) > 0)
    % log10(2) = 0.3 so set slightly lower
    n1(i) = 0.2;
  end
end

i = find(~isfinite(n1));
if(length(i) > 0)
  if(exist('OCTAVE_VERSION', 'builtin') ~= 0)
    n1(i) = 0;
  else
    % In Matlab setting to NaN sets the data transparent
    n1(i) = NaN;
  end
end

h = pcolor(xb,yb,n1);
set(h, 'EdgeColor', 'none');

CM = colormap('jet');
% In Matlab pcolor plots NaN as transparent. In Octave it plots the
% NaN's as the first colour in the colormap. Tweak the colormap to
% set the first colour white
if(exist('OCTAVE_VERSION', 'builtin') ~= 0)
  CM(1,1:3) = 1;
  colormap(CM);
end

xlabel('2D Error [m]'); 
ylabel('2D 1-\sigma Error Estimate [m]');

grid;
h = colorbar;
if(exist('OCTAVE_VERSION', 'builtin') ~= 0)
  set(get(h,'Ylabel'),'String','log10(Number of Points)','FontSize',20,'FontName','Arial');
else
  set(get(h,'Ylabel'),'String','log10(Number of Points)','FontSize',12,'FontName','Arial');
end

hold on
if(exist('OCTAVE_VERSION', 'builtin') ~= 0)
  plot(0:0.05:MaxErr,0:0.05:MaxErr,'r','linewidth',10)
else
  plot(0:0.05:MaxErr,0:0.05:MaxErr,'r','linewidth',3)
end

axis square
title(titleStr);

if(exist('OCTAVE_VERSION', 'builtin') ~= 0)
  print('-dpng',[filePrefix,'-errPlot.png'],'-S960,720');
else
  print('-dpng',[filePrefix,'-errPlot.png']);
end


