2
votes

I have several subplots in a for loop of subjects. I want to plot the subject wise performance from a 20x2 table. How do I make a separate subplot- a barchart on the same figure in a way that at first iteration it updates and plot the elements of 1st row as Space and Time respectively, then on 2nd iteration that of second row and so on, from the performance table? An excerpt from my code is given below.

Thanks in advance for your help!

Best, A

for s=1:size(SubjectFolder,2)
                    % ..... some code
figure(1)
 subplot(423)
                        f_psd = Grand_AvgTF_Space.freq;
                        [trash,f_psd_plot] = min(abs(f_psd-which_f)); 
                        PSD = mean(abs(squeeze(Grand_AvgTF_Space.powspctrm(s,:,:,:))),3); % [ch x freq] PSD of space or time condition          
                        semilogy(f_psd,PSD,'color','b')
                        hold on, 
                        semilogy(f_psd, mean(PSD),'linewidth',3,'color','r')
                        hold on
                        line([which_f which_f],[min(PSD(:)) max(PSD(:))],'color','g','linewidth',3)
                        xlabel('Frequency (Hz)')
                        ylabel('Power (dB/Hz)')
                        title( 'Space' )
                        ylim([min(PSD(:)) max(PSD(:))])
                        title( 'Space' )
    subplot(424), % topoplot space
                    data_megin.avg = repmat(PSD(:,f_psd_plot),1,length(data_megin.time));
                    cfg.parameter        = 'avg' %
                    ft_topoplotER(cfg, data_megin );
                    title(['Topolot for Space Freq = ' num2str(f_psd(f_psd_plot)) ' Hz'])
                    
                    %..... some code
            end 
        
            % 20x2 Table looks like this without variable names. Suppose I wanna give 1st col name as Space and 2nd col as Time in the required subplot, where each row represents subject1 to subject20.
            96   100
            85   89
            .     .
            .     .
            80    87

enter image description here

enter image description here

2

2 Answers

1
votes

Something on the lines of this might work, Unfortunately, without the data I resorted to using placeholder plots. The size and colours of the bar graph can most likely be changed as well. Here I assume the topographical plots can take a subplot slot without any issues. I'm not familiar enough with the plotting characteristics/behaviour of topographical plots to judge if this will work as expected.

For a smaller bar graph change this line from:

subplot(4,3,[3 6 9 12]); Bar_Graph = bar(Bar_Data);

to:

subplot(4,3,[6 9]); Bar_Graph = bar(Bar_Data);

Subplots Using Multiple Slots

Table = round(100*rand(20,2));
Placeholder = zeros(500,1000);

for Figure_Index = 1: 20

figure(Figure_Index);
subplot(4,3,1); imshow(Placeholder); title("Plot 1");
subplot(4,3,2); imshow(Placeholder); title("Topoplot 1");
subplot(4,3,4); imshow(Placeholder); title("Plot 2");
subplot(4,3,5); imshow(Placeholder); title("Topoplot 2");
subplot(4,3,7); imshow(Placeholder); title("Plot 3");
subplot(4,3,8); imshow(Placeholder); title("Topoplot 3");
subplot(4,3,10); imshow(Placeholder); title("Plot 4");
subplot(4,3,11); imshow(Placeholder); title("Topoplot 4");

Bar_Data = Table(Figure_Index,:);
subplot(4,3,[3 6 9 12]); Bar_Graph = bar(Bar_Data);
text(1:length(Bar_Data),Bar_Data,num2str(Bar_Data'),'vert','bottom','horiz','center'); 
Bar_Graph.BaseLine.Visible = 'off';
axis off

end

Ran using MATLAB R2019b

1
votes

Indicating Axes in bar() and plot() Calls

This might not exactly fit your exact use case but if you're simply looking to add a bar to a bar graph that is a part of a subplot on every iteration this example might help. Here the subplot(4,2,3) axes are assigned to the variable Bar_Graph_Axes. Bar_Graph_Axes can be called within the bar() call to indicate the specific axes to plot on. Passing axes names is a good way to modify existing plots since calling hold on will automatically allow you to modify the last called plot. Below is a playground script that you may modify to fit your implementation. The pause(1) is there for visualization purposes to show each bar being added.

Bar Graph Subplot

Playground Script:

Number_Of_Graphs = 20;

Table = rand(20,2);
Table(:,1) = (1:20);

for Graph_Index = 1: Number_Of_Graphs
    
Bar_Graph_Axes = subplot(4,2,3); 
hold on
bar(Bar_Graph_Axes,Table(Graph_Index,1),Table(Graph_Index,2));
title("Bar Graph Subplot");
pause(1);

end

If Data is Already Formatted in Array

If the data is simply in an array where the categories are defined by the columns calling the function bar() will automatically make a double bar graph in this case.

Double Bar Graph

Number_Of_Iterations = 20;
Performance_Space_Time = rand(Number_Of_Iterations,2);
bar(Performance_Space_Time);
xticks(1:Number_Of_Iterations);
title("Bar Graph");
xlabel("Iteration, s"); ylabel("Value");
legend("Space","Time");

Ran using MATLAB R2019b