
function [counts,histogram_centers]=plot_color_bar_and_floe(color_limit_N,MCD,Y_limi,N,Poly_struc,length_over_Pixel)

x_bin = [1:1:100];
[counts,histogram_centers]=hist(MCD,x_bin);

%---we need to create a biased stations to highlight the color
%difference---

% color_limit_N = 30; % the first 30 bars are colored


% N.B. there are two can be confusing variable here:
%       1. histogram center
%       2. floe center

color_M = jet(color_limit_N);



figure,
hold on
for i = 1:1:size(histogram_centers,2)
    %color_vec(i,:) = [min((centers(i)-C_min)/C_dist,1) 0 max(-(centers(i)-C_max)/C_dist,0)];
    
    h(i) = bar(histogram_centers(i),counts(i),'BarWidth', 1);
    if i<=color_limit_N
        set(h(i),'FaceColor',color_M(i,:));
    else
        set(h(i),'FaceColor',color_M(color_limit_N,:));
    end
end
hold off
colormap(color_M)
caxis([histogram_centers(1) histogram_centers(color_limit_N)])
colorbar

set(gcf, 'Color','w')
set(gca,'Box','off','Fontsize',18)
xlabel(gca,'MCD $L_{i}$ [m]','Fontsize',25,'interpreter','Latex');
ylabel(gca,'Number of ice floes $N$','Fontsize',25,'interpreter','Latex');
grid on
axis tight



figure,
hold on
for i = 1:1:N 
    clear x y
    [~,index]=min(abs(MCD(i)-histogram_centers));
    
    if isfield(Poly_struc,'Vertices')
        x = Poly_struc(i).Vertices(:,1);
        y = Y_limi-Poly_struc(i).Vertices(:,2);
        
        x = x*length_over_Pixel;
        y = y*length_over_Pixel;
        
        
        %---the plot method is different------
        %---using the cmd fill to plot polygon---
       
        

        if index<=color_limit_N
            fill(x,y, color_M(index,:))
        else
            fill(x,y, color_M(color_limit_N,:))
        end
        
        
    elseif isfield(Poly_struc,'Pixels')
        x = Poly_struc(i).Pixels(:,1); % they are ordered in a different manner
        y = Poly_struc(i).Pixels(:,2);
        
        for j=1:1:size(x,1)
             if index<=color_limit_N
                rgbImage(y(j),x(j), :) = color_M(index,:); % r, g, b are uint8 values.
             else
                rgbImage(y(j),x(j), :) = color_M(color_limit_N,:); % r, g, b are uint8 values.
             end
        end
        imshow(rgbImage);
        
        
    else
        error('the field name is incorrect')
    end
end
hold off
set(gcf, 'Color','w')
set(gca,'Box','off','Fontsize',18)

xlabel(gca,'$X$ [m]','Fontsize',25,'interpreter','Latex');
ylabel(gca,'$Y$ [m]','Fontsize',25,'interpreter','Latex');


axis equal
axis tight

for i = 1:1:N 
    centre(i,:)=Poly_struc(i).Center;
    hold on
    if isfield(Poly_struc,'Vertices')
        plot(centre(i,1)*length_over_Pixel,Y_limi*length_over_Pixel-centre(i,2)*length_over_Pixel,'w.','LineWidth',1.2)
    elseif isfield(Poly_struc,'Pixels')
        plot(centre(i,1),centre(i,2),'w.','LineWidth',1.2)
    end
        
end

colormap(color_M)
caxis([histogram_centers(1) histogram_centers(color_limit_N)])
colorbar
end