clear all; clc
% model written by Allison Shaw (contact for assistance: ashaw@umn.edu)
% started April 2021
%   last updated: 10 January 2023
%
% plots two sets of simulations -- one where spatial sorting slows down spread,
% and one where spatial sorting speeds up spread.

load fig23sims.mat

tmp = flipud(parula);
colors = tmp(1:25:end,:); % color scheme

fs1 = 10;  % axes labels
fs3 = 09;  % axis numbering
lw2 = 1; % fig edges
lw1 = 1.5; % figure lines

width = 10;
height = 13;
xpos = 3;
ypos = 2;
sx = 0.12;
sy = 0.15;
w = 0.34;
he = 0.2;
dy = 0.1;
dx = 0.16;

figure(1); clf
hh = gcf;
set(hh,'PaperUnits','centimeters');
set(hh,'Units','centimeters');
set(gcf,'Position',[xpos ypos width height])


ind = [1 5 10];
for i=1:length(ind)
    
    axes('position',[sx sy+(3-i)*(dy+he) w he])
        plot(x_with_lo,squeeze(n_with_lo(ind(i),:,:)),'LineWidth',lw1);
        set(gca,'ColorOrder',colors)
        xlim([0 4])
        if i==2; ylim([0 0.25]); end
        ylabel('density','fontsize',fs1)
        xlabel('space','fontsize',fs1)
        set(gca,'FontSize',fs3,'LineWidth',lw2,'Fontname', 'Arial');

    axes('position',[sx+dx+w sy+(3-i)*(dy+he) w he])
        plot(x_with_hi,squeeze(n_with_hi(ind(i),:,:)),'LineWidth',lw1);
        set(gca,'ColorOrder',colors)
        xlim([0 4])
        ylabel('density','fontsize',fs1)
        xlabel('space','fontsize',fs1)
        set(gca,'FontSize',fs3,'LineWidth',lw2,'Fontname', 'Arial');
end


axes('position',[0.37 0.04 0.3 0.03])
    imagesc(fliplr(1:10))
    set(gca,'XTick',[1 5 10]); set(gca,'XTickLabel',{'0.1','0.5','1'});
    set(gca,'YTick',[]); set(gca,'YTickLabel',{});
    set(gca,'FontSize',fs3,'LineWidth',lw2,'Fontname', 'Arial');
    
% label subpanels
axes('position',[0 0 1 1],'visible','off')
hold on
     text(0.02,0.36+2*(dy+he),'a)')
     text(0.02,0.36+1*(dy+he),'b)')
     text(0.02,0.36+0*(dy+he),'c)')
     text(0.52,0.36+2*(dy+he),'d)')
     text(0.52,0.36+1*(dy+he),'e)')
     text(0.52,0.36+0*(dy+he),'f)')
     %
     for i = 1:3
         text(0.45,0.33+(3-i)*(dy+he),strcat(['t = ' num2str(ind(i))]),'horizontalalignment','right')
         text(0.95,0.33+(3-i)*(dy+he),strcat(['t = ' num2str(ind(i))]),'horizontalalignment','right')
     end
     %
     set(gca,'FontSize',fs3,'Fontname', 'Arial');
axis([0 1 0 1])

% Backup previous settings
prePaperType = get(hh,'PaperType');
prePaperPosition = get(hh,'PaperPosition');
prePaperSize = get(hh,'PaperSize');

% Make changing paper type possible
set(hh,'PaperType','<custom>');
% Set the page size and position to match the figure's dimensions
position = get(hh,'Position');
set(hh,'PaperPosition',[0,0,position(3:4)]);
set(hh,'PaperSize',position(3:4));

%print -dpdf -r600 Figure1.pdf
print -djpeg -r600 fig2.jpg
print -depsc fig2.eps

% Restore the previous settings
set(hh,'PaperType',prePaperType);
set(hh,'PaperPosition',prePaperPosition);
set(hh,'PaperSize',prePaperSize);