f = figure();
% use colormap parula because it goes blue -> yellow:
cmap = parula();
% define the color limits:
clim = [-10.4, 1.3];
% define the values to use for line colors:
vals = [-10.4, -6, -2.3, 0.3, 1.3];
% indices into cmap of vals, using clim as the upper and lower limits:
idx = 1+round((vals-clim(1))/(clim(2)-clim(1))*(size(cmap,1)-1));
% colors from colormap at those indices:
line_colors = cmap(idx,:);
% plot (random) lines with those colors:
hold on
for ii = 1:numel(vals)
plot(0:0.5:4.5,rand(1,10),'Color',line_colors(ii,:),'LineWidth',1.5);
end
% create the legend:
legend("-10.4","-6","-2.3","0.3","1.3")
% create the colorbar:
set(f,'Colormap',cmap);
caxis(clim)
colorbar();