【问题标题】:PCA with colorbar带彩条的 PCA
【发布时间】:2016-05-24 12:49:15
【问题描述】:

我有this data,我想对其进行主成分分析。

特别是对于我想要关联颜色的每个数据点。

这是我的代码:

    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'])

结果图如下:

除了颜色条范围外,一切都很好。事实上decstd 的值介于 0 和 2 之间。实际上我根本不明白颜色条上的值是什么。 有人明白吗?

是否可以恢复颜色栏中的数据?那么要了解它们是什么?

【问题讨论】:

  • 什么是hbi?它在哪里声明/分配给?

标签: matlab pca colorbar


【解决方案1】:
size(autumn)

显示autumn 颜色图(实际上是所有颜色图)的默认长度为64。当您调用colorbar 时,默认情况下它将使用从1n 的刻度标签,其中n 是颜色图的长度,在本例中为64

如果您希望颜色条刻度标签的映射与您用来使数据适合 164 之间的映射相匹配(即您的这一行 indCol = ceil( size(cm,1) * abs(metric(userdata))/max(abs(metric)) );),那么您需要设置自己喜欢这样

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 );

【讨论】:

  • 谢谢!我解决了这个问题:caxis([min(metric), max(metric)]) set(c,'YTick',linspace(min(metric), max(metric),6));
  • 我会将其添加到答案中
猜你喜欢
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 2016-02-28
  • 2019-12-06
相关资源
最近更新 更多