Boxplot in a specified position in a subplot
    2 ビュー (過去 30 日間)
  
       古いコメントを表示
    
Dear
I have a subplot which (is quite complicate). Precisely, it takes values from a table and results in plotting subplots where each subplot has a double plot: on the right I plot a line series representing an histogram for 5 values of variable kimpute, while on the right I plot a bar
The code is below. It works and achieves the attached plot. May anyone tell me how may I insert a boxplot on the right of the bar??
    normPmean = min(AllPmeans(:));
    normColor = max(AllPmeans(:));
    WSIMS = [0.5 0.75 0.9 1 1.25];
    props = 1:0.5:4
    strDataset = 'DBele';
    indDB = 1;
                fig = figure('Name', ...
                    ['Selected k by knn imputation (' strDataset ' dataset)']);
                title(['Selected k by knn imputation (' strDataset ' dataset)'])
                rows = find(AllkIMPUTE.DB == indDB); % take values from that dataset
                % take values to be plotted
                subTab = table(AllkIMPUTE( rows ,:).unbF, AllkIMPUTE( rows ,:).wsame, ...
                            AllkIMPUTE( rows ,:).kimpute, AllkIMPUTE( rows ,:).pmeannearestToMin);
                subTab.Properties.VariableNames = {'unbF','wsame', 'kimpute','pmean'};
                fig = figure('units','normalized','outerposition',[0 0 1 1]);
                N=108; % number of samples
                kPerUnbFWsame = NaN(N,numel(props),numel(WSIMS));
                ind = 1;
                indWsim = 0;
                pmeans = zeros(numel(props)*numel(WSIMS),1); % parameter to rule the subplot
                for ww = WSIMS
                    %kPerWsame = zeros(N-2, numel(props));
                    indUnbF = 0;
                    indWsim = indWsim +1;
                    for unbF = props              
                        indUnbF = indUnbF+1;    
                        rowSub = subTab.unbF == unbF & subTab.wsame == ww;
                        kPerUnbFWsame(:,indUnbF,indWsim) = subTab(rowSub,:).kimpute; 
                        hold on; h = subplot(numel(WSIMS),numel(props),ind);
                        % compute my small histogram
                        hMio = zeros(numel(kimpute),1);
                        for kk=kimpute
                            hMio(kk) = numel(find(subTab(rowSub,:).kimpute==kk));
                        end
                        % find k that gives the maximum value in the histogram
                        [~, indk] = max(hMio);
                        rowSub = subTab.unbF == unbF & subTab.wsame == ww & subTab.kimpute == kimpute(indk);
                        % find the pmean related to maximum k
                        sz=round(mean(subTab(rowSub,:).pmean),4);
                        szBox = subTab(rowSub,:).pmean;
                        pmeans(ind) = sz;
                        % NOW ON THE LEFT PLOT THE SMALL HISTOGRAM
                        plot(kimpute,hMio);
                        if sz<0.1
                            szNew = round(sz*5000);
                            % ON THE RIGHT PLOT THE BAR                            
                            hold on; bar(max(kimpute)+1, 100*(normPmean/sz),0.3, 'FaceColor', [round(1-sz/normColor,2) 0 round(1-sz/normColor,2)])
                            if mod(ind,numel(props))==1 || mod(ind,numel(props))==0 
                                text(max(kimpute),100*(normPmean/sz)+8, ['10^{-4}' num2str(sz*10^4)], 'FontSize',8); end
                        end
                        if (mod(ind, numel(props))==1)
                            ylabel(['w_{same} = ' num2str(ww)]); end
                        if ind>numel(props)*(numel(WSIMS)-1)
                            xlabel(['unbF = ' num2str(unbF)]); end
                        set(h,'XTick', kimpute);
                        set(h,'XTickLabel', cellstr(num2str([kimpute]')));
                        set(h,'XLim', [0, max(kimpute)+1.5]);
                        set(h,'YTick', max(hMio));
                        set(h,'YTickLabel', cellstr(num2str(max(hMio))));
                        set(h,'YLim', [0, N+2]);
                        ind = ind+1;
                    end    
                    end
                suplabel(['mean p for ' strDataset ' = ' num2str(mean(pmeans'))],'t')
                saveas(fig,[dirSave filesep strDataset 'kimpute.jpg']);
            end
0 件のコメント
採用された回答
  dpb
      
      
 2019 年 10 月 27 日
        ...
bar(max(kimpute)+1, ...
% add boxplot onto right end of axis
NX=max(kimpute)+1;                  % how many locations on x axis plotted
bxplotdata=...                      % whatever it is that is wanted as the data
NY=numel(bxplotdata);               % how many points are there -- assume is column vector
boxplot([nan(NY,NX+1) bxplotdata])  % add the boxplot with dummy placeholders for locations
...
  There is no X axis variable for boxplot so you have to create enough dummy columns that the data are plotted where wanted on the x axis.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

