Search code examples
matlabpcacolorbar

PCA with colorbar


I have this data of which I want to make a principal component analysis.

In particular for each data point I want to associate a color.

This is my code:

    for ii=1:size(SBF_ens,1)
        SBF(ii) = SBF_ens(ii, max(find(SBF_ens(ii,:)~=0)) );  %value at the moment of the measurement
    end

    %matrix of data
    toPCA =[
        wind_trend_72h_ens-nanmean(wind_trend_72h_ens);
        wind_trend_24h_ens-nanmean(wind_trend_24h_ens);
        wind_trend_12to18h_ens-nanmean(wind_trend_12to18h_ens);
        wind_trend_0to12h_ens-nanmean(wind_trend_0to12h_ens);
        wind_trend_last6h_ens-nanmean(wind_trend_last6h_ens);
        Mwind12h_ens-nanmean(Mwind12h_ens);
        Mwind24h_ens-nanmean(Mwind24h_ens);
        SBF-nanmean(SBF)]';

    variables = { 'wt72h','wt24h','wt12to18h','wt0to12h','wtLast6h','Mw12h', 'Mw24h', 'SBF'}; %labels 

    %PCA algorithm    
    C = corr(toPCA,toPCA);   

    w = 1./var(toPCA);

    [wcoeff,score,latent,tsquared,explained] = pca(toPCA,'VariableWeights',w);

    coefforth = diag(sqrt(w))*wcoeff;

    metric=decstd_ens; %metric for colorbar

hbi=biplot(coefforth(:,1:2),'scores',score(:,1:2),'varlabels',...
    variables,'ObsLabels', num2str([1:length(toPCA)]'),...
'markersize', 15);

    %plotting
    cm = autumn;
    colormap(cm);


    for ii = length(hbi)-length(toPCA):length(hbi)
        userdata = get(hbi(ii), 'UserData');

        if ~isempty(userdata)

             indCol = ceil( size(cm,1) * abs(metric(userdata))/max(abs(metric)) );%maps decstd between 0 and 1 and find the colormap index


            if indCol==0     %just avoid 0                    
                indCol=1;
            end

            col = cm(indCol,:); %color corresponding to the index

                set(hbi(ii), 'Color', col); %modify the dot's color


        end
    end
    for ii = 1:length(hbi)-length(toPCA)-1 %dots corresponding to the original dimensions are in black
        set(hbi(ii), 'Color', 'k');
    end

    c=colorbar;
    ylabel(c,'decstd') %is this true?
    xlabel(['1^{st} PCA component ',  num2str(explained(1)), ' of variance explained'])
    ylabel(['2^{nd} PCA component ', num2str(explained(2)), ' of variance explained'])

The resulting figure is the following:

Everything is fine except for the colorbar range. In fact decstd has values between 0 and 2. Actually I do not understand at all what the values on the colorbar are. Does anyone understand it?

Is it possible to rethrive the data in the colorbar? So to understand what they are?


Solution

  • size(autumn)
    

    shows you that the default length of the autumn colourmap (actually of all the colourmaps) is 64. When you call colorbar, by default it will use tick labels from 1 to n where n is the length of your colourmap, in this case 64.

    If you want the mapping of the colourbar ticklabels to match the mapping that you used to get your data to fit between 1 and 64 (i.e. this line of yours indCol = ceil( size(cm,1) * abs(metric(userdata))/max(abs(metric)) );), then you will need to set that yourself like this

    numTicks = 6;
    cAxisTicks = linspace(min(metric), max(metric), numTicks); % or whatever the correct limits are for your data
    caxis([min(metric), max(metric)]);
    set(c,'YTick', cAxisTicks );